Pull thermal into release branch
[linux-2.6] / fs / ocfs2 / file.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * file.c
5  *
6  * File open, close, extend, truncate
7  *
8  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 021110-1307, USA.
24  */
25
26 #include <linux/capability.h>
27 #include <linux/fs.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #include <linux/pagemap.h>
32 #include <linux/uio.h>
33 #include <linux/sched.h>
34 #include <linux/pipe_fs_i.h>
35 #include <linux/mount.h>
36 #include <linux/writeback.h>
37
38 #define MLOG_MASK_PREFIX ML_INODE
39 #include <cluster/masklog.h>
40
41 #include "ocfs2.h"
42
43 #include "alloc.h"
44 #include "aops.h"
45 #include "dir.h"
46 #include "dlmglue.h"
47 #include "extent_map.h"
48 #include "file.h"
49 #include "sysfile.h"
50 #include "inode.h"
51 #include "ioctl.h"
52 #include "journal.h"
53 #include "mmap.h"
54 #include "suballoc.h"
55 #include "super.h"
56
57 #include "buffer_head_io.h"
58
59 static int ocfs2_sync_inode(struct inode *inode)
60 {
61         filemap_fdatawrite(inode->i_mapping);
62         return sync_mapping_buffers(inode->i_mapping);
63 }
64
65 static int ocfs2_file_open(struct inode *inode, struct file *file)
66 {
67         int status;
68         int mode = file->f_flags;
69         struct ocfs2_inode_info *oi = OCFS2_I(inode);
70
71         mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
72                    file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
73
74         spin_lock(&oi->ip_lock);
75
76         /* Check that the inode hasn't been wiped from disk by another
77          * node. If it hasn't then we're safe as long as we hold the
78          * spin lock until our increment of open count. */
79         if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
80                 spin_unlock(&oi->ip_lock);
81
82                 status = -ENOENT;
83                 goto leave;
84         }
85
86         if (mode & O_DIRECT)
87                 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
88
89         oi->ip_open_count++;
90         spin_unlock(&oi->ip_lock);
91         status = 0;
92 leave:
93         mlog_exit(status);
94         return status;
95 }
96
97 static int ocfs2_file_release(struct inode *inode, struct file *file)
98 {
99         struct ocfs2_inode_info *oi = OCFS2_I(inode);
100
101         mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
102                        file->f_path.dentry->d_name.len,
103                        file->f_path.dentry->d_name.name);
104
105         spin_lock(&oi->ip_lock);
106         if (!--oi->ip_open_count)
107                 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
108         spin_unlock(&oi->ip_lock);
109
110         mlog_exit(0);
111
112         return 0;
113 }
114
115 static int ocfs2_sync_file(struct file *file,
116                            struct dentry *dentry,
117                            int datasync)
118 {
119         int err = 0;
120         journal_t *journal;
121         struct inode *inode = dentry->d_inode;
122         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
123
124         mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
125                    dentry->d_name.len, dentry->d_name.name);
126
127         err = ocfs2_sync_inode(dentry->d_inode);
128         if (err)
129                 goto bail;
130
131         journal = osb->journal->j_journal;
132         err = journal_force_commit(journal);
133
134 bail:
135         mlog_exit(err);
136
137         return (err < 0) ? -EIO : 0;
138 }
139
140 int ocfs2_should_update_atime(struct inode *inode,
141                               struct vfsmount *vfsmnt)
142 {
143         struct timespec now;
144         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
145
146         if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
147                 return 0;
148
149         if ((inode->i_flags & S_NOATIME) ||
150             ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
151                 return 0;
152
153         /*
154          * We can be called with no vfsmnt structure - NFSD will
155          * sometimes do this.
156          *
157          * Note that our action here is different than touch_atime() -
158          * if we can't tell whether this is a noatime mount, then we
159          * don't know whether to trust the value of s_atime_quantum.
160          */
161         if (vfsmnt == NULL)
162                 return 0;
163
164         if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
165             ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
166                 return 0;
167
168         if (vfsmnt->mnt_flags & MNT_RELATIME) {
169                 if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
170                     (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
171                         return 1;
172
173                 return 0;
174         }
175
176         now = CURRENT_TIME;
177         if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
178                 return 0;
179         else
180                 return 1;
181 }
182
183 int ocfs2_update_inode_atime(struct inode *inode,
184                              struct buffer_head *bh)
185 {
186         int ret;
187         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
188         handle_t *handle;
189
190         mlog_entry_void();
191
192         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
193         if (handle == NULL) {
194                 ret = -ENOMEM;
195                 mlog_errno(ret);
196                 goto out;
197         }
198
199         inode->i_atime = CURRENT_TIME;
200         ret = ocfs2_mark_inode_dirty(handle, inode, bh);
201         if (ret < 0)
202                 mlog_errno(ret);
203
204         ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
205 out:
206         mlog_exit(ret);
207         return ret;
208 }
209
210 static int ocfs2_set_inode_size(handle_t *handle,
211                                 struct inode *inode,
212                                 struct buffer_head *fe_bh,
213                                 u64 new_i_size)
214 {
215         int status;
216
217         mlog_entry_void();
218         i_size_write(inode, new_i_size);
219         inode->i_blocks = ocfs2_inode_sector_count(inode);
220         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
221
222         status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
223         if (status < 0) {
224                 mlog_errno(status);
225                 goto bail;
226         }
227
228 bail:
229         mlog_exit(status);
230         return status;
231 }
232
233 static int ocfs2_simple_size_update(struct inode *inode,
234                                     struct buffer_head *di_bh,
235                                     u64 new_i_size)
236 {
237         int ret;
238         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
239         handle_t *handle = NULL;
240
241         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
242         if (handle == NULL) {
243                 ret = -ENOMEM;
244                 mlog_errno(ret);
245                 goto out;
246         }
247
248         ret = ocfs2_set_inode_size(handle, inode, di_bh,
249                                    new_i_size);
250         if (ret < 0)
251                 mlog_errno(ret);
252
253         ocfs2_commit_trans(osb, handle);
254 out:
255         return ret;
256 }
257
258 static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
259                                      struct inode *inode,
260                                      struct buffer_head *fe_bh,
261                                      u64 new_i_size)
262 {
263         int status;
264         handle_t *handle;
265         struct ocfs2_dinode *di;
266
267         mlog_entry_void();
268
269         /* TODO: This needs to actually orphan the inode in this
270          * transaction. */
271
272         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
273         if (IS_ERR(handle)) {
274                 status = PTR_ERR(handle);
275                 mlog_errno(status);
276                 goto out;
277         }
278
279         status = ocfs2_journal_access(handle, inode, fe_bh,
280                                       OCFS2_JOURNAL_ACCESS_WRITE);
281         if (status < 0) {
282                 mlog_errno(status);
283                 goto out_commit;
284         }
285
286         /*
287          * Do this before setting i_size.
288          */
289         status = ocfs2_zero_tail_for_truncate(inode, handle, new_i_size);
290         if (status) {
291                 mlog_errno(status);
292                 goto out_commit;
293         }
294
295         i_size_write(inode, new_i_size);
296         inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
297         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
298
299         di = (struct ocfs2_dinode *) fe_bh->b_data;
300         di->i_size = cpu_to_le64(new_i_size);
301         di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
302         di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
303
304         status = ocfs2_journal_dirty(handle, fe_bh);
305         if (status < 0)
306                 mlog_errno(status);
307
308 out_commit:
309         ocfs2_commit_trans(osb, handle);
310 out:
311
312         mlog_exit(status);
313         return status;
314 }
315
316 static int ocfs2_truncate_file(struct inode *inode,
317                                struct buffer_head *di_bh,
318                                u64 new_i_size)
319 {
320         int status = 0;
321         struct ocfs2_dinode *fe = NULL;
322         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
323         struct ocfs2_truncate_context *tc = NULL;
324
325         mlog_entry("(inode = %llu, new_i_size = %llu\n",
326                    (unsigned long long)OCFS2_I(inode)->ip_blkno,
327                    (unsigned long long)new_i_size);
328
329         truncate_inode_pages(inode->i_mapping, new_i_size);
330
331         fe = (struct ocfs2_dinode *) di_bh->b_data;
332         if (!OCFS2_IS_VALID_DINODE(fe)) {
333                 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
334                 status = -EIO;
335                 goto bail;
336         }
337
338         mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
339                         "Inode %llu, inode i_size = %lld != di "
340                         "i_size = %llu, i_flags = 0x%x\n",
341                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
342                         i_size_read(inode),
343                         (unsigned long long)le64_to_cpu(fe->i_size),
344                         le32_to_cpu(fe->i_flags));
345
346         if (new_i_size > le64_to_cpu(fe->i_size)) {
347                 mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
348                      (unsigned long long)le64_to_cpu(fe->i_size),
349                      (unsigned long long)new_i_size);
350                 status = -EINVAL;
351                 mlog_errno(status);
352                 goto bail;
353         }
354
355         mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
356              (unsigned long long)le64_to_cpu(fe->i_blkno),
357              (unsigned long long)le64_to_cpu(fe->i_size),
358              (unsigned long long)new_i_size);
359
360         /* lets handle the simple truncate cases before doing any more
361          * cluster locking. */
362         if (new_i_size == le64_to_cpu(fe->i_size))
363                 goto bail;
364
365         /* This forces other nodes to sync and drop their pages. Do
366          * this even if we have a truncate without allocation change -
367          * ocfs2 cluster sizes can be much greater than page size, so
368          * we have to truncate them anyway.  */
369         status = ocfs2_data_lock(inode, 1);
370         if (status < 0) {
371                 mlog_errno(status);
372                 goto bail;
373         }
374
375         /* alright, we're going to need to do a full blown alloc size
376          * change. Orphan the inode so that recovery can complete the
377          * truncate if necessary. This does the task of marking
378          * i_size. */
379         status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
380         if (status < 0) {
381                 mlog_errno(status);
382                 goto bail_unlock_data;
383         }
384
385         status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
386         if (status < 0) {
387                 mlog_errno(status);
388                 goto bail_unlock_data;
389         }
390
391         status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
392         if (status < 0) {
393                 mlog_errno(status);
394                 goto bail_unlock_data;
395         }
396
397         /* TODO: orphan dir cleanup here. */
398 bail_unlock_data:
399         ocfs2_data_unlock(inode, 1);
400
401 bail:
402
403         mlog_exit(status);
404         return status;
405 }
406
407 /*
408  * extend allocation only here.
409  * we'll update all the disk stuff, and oip->alloc_size
410  *
411  * expect stuff to be locked, a transaction started and enough data /
412  * metadata reservations in the contexts.
413  *
414  * Will return -EAGAIN, and a reason if a restart is needed.
415  * If passed in, *reason will always be set, even in error.
416  */
417 int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
418                                struct inode *inode,
419                                u32 *logical_offset,
420                                u32 clusters_to_add,
421                                struct buffer_head *fe_bh,
422                                handle_t *handle,
423                                struct ocfs2_alloc_context *data_ac,
424                                struct ocfs2_alloc_context *meta_ac,
425                                enum ocfs2_alloc_restarted *reason_ret)
426 {
427         int status = 0;
428         int free_extents;
429         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
430         enum ocfs2_alloc_restarted reason = RESTART_NONE;
431         u32 bit_off, num_bits;
432         u64 block;
433
434         BUG_ON(!clusters_to_add);
435
436         free_extents = ocfs2_num_free_extents(osb, inode, fe);
437         if (free_extents < 0) {
438                 status = free_extents;
439                 mlog_errno(status);
440                 goto leave;
441         }
442
443         /* there are two cases which could cause us to EAGAIN in the
444          * we-need-more-metadata case:
445          * 1) we haven't reserved *any*
446          * 2) we are so fragmented, we've needed to add metadata too
447          *    many times. */
448         if (!free_extents && !meta_ac) {
449                 mlog(0, "we haven't reserved any metadata!\n");
450                 status = -EAGAIN;
451                 reason = RESTART_META;
452                 goto leave;
453         } else if ((!free_extents)
454                    && (ocfs2_alloc_context_bits_left(meta_ac)
455                        < ocfs2_extend_meta_needed(fe))) {
456                 mlog(0, "filesystem is really fragmented...\n");
457                 status = -EAGAIN;
458                 reason = RESTART_META;
459                 goto leave;
460         }
461
462         status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
463                                       &bit_off, &num_bits);
464         if (status < 0) {
465                 if (status != -ENOSPC)
466                         mlog_errno(status);
467                 goto leave;
468         }
469
470         BUG_ON(num_bits > clusters_to_add);
471
472         /* reserve our write early -- insert_extent may update the inode */
473         status = ocfs2_journal_access(handle, inode, fe_bh,
474                                       OCFS2_JOURNAL_ACCESS_WRITE);
475         if (status < 0) {
476                 mlog_errno(status);
477                 goto leave;
478         }
479
480         block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
481         mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
482              num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
483         status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
484                                      *logical_offset, block, num_bits,
485                                      meta_ac);
486         if (status < 0) {
487                 mlog_errno(status);
488                 goto leave;
489         }
490
491         status = ocfs2_journal_dirty(handle, fe_bh);
492         if (status < 0) {
493                 mlog_errno(status);
494                 goto leave;
495         }
496
497         clusters_to_add -= num_bits;
498         *logical_offset += num_bits;
499
500         if (clusters_to_add) {
501                 mlog(0, "need to alloc once more, clusters = %u, wanted = "
502                      "%u\n", fe->i_clusters, clusters_to_add);
503                 status = -EAGAIN;
504                 reason = RESTART_TRANS;
505         }
506
507 leave:
508         mlog_exit(status);
509         if (reason_ret)
510                 *reason_ret = reason;
511         return status;
512 }
513
514 /*
515  * For a given allocation, determine which allocators will need to be
516  * accessed, and lock them, reserving the appropriate number of bits.
517  *
518  * Called from ocfs2_extend_allocation() for file systems which don't
519  * support holes, and from ocfs2_write() for file systems which
520  * understand sparse inodes.
521  */
522 int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
523                           u32 clusters_to_add,
524                           struct ocfs2_alloc_context **data_ac,
525                           struct ocfs2_alloc_context **meta_ac)
526 {
527         int ret, num_free_extents;
528         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
529
530         *meta_ac = NULL;
531         *data_ac = NULL;
532
533         mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
534              "clusters_to_add = %u\n",
535              (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
536              le32_to_cpu(di->i_clusters), clusters_to_add);
537
538         num_free_extents = ocfs2_num_free_extents(osb, inode, di);
539         if (num_free_extents < 0) {
540                 ret = num_free_extents;
541                 mlog_errno(ret);
542                 goto out;
543         }
544
545         /*
546          * Sparse allocation file systems need to be more conservative
547          * with reserving room for expansion - the actual allocation
548          * happens while we've got a journal handle open so re-taking
549          * a cluster lock (because we ran out of room for another
550          * extent) will violate ordering rules.
551          *
552          * Most of the time we'll only be seeing this 1 cluster at a time
553          * anyway.
554          */
555         if (!num_free_extents ||
556             (ocfs2_sparse_alloc(osb) && num_free_extents < clusters_to_add)) {
557                 ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
558                 if (ret < 0) {
559                         if (ret != -ENOSPC)
560                                 mlog_errno(ret);
561                         goto out;
562                 }
563         }
564
565         ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
566         if (ret < 0) {
567                 if (ret != -ENOSPC)
568                         mlog_errno(ret);
569                 goto out;
570         }
571
572 out:
573         if (ret) {
574                 if (*meta_ac) {
575                         ocfs2_free_alloc_context(*meta_ac);
576                         *meta_ac = NULL;
577                 }
578
579                 /*
580                  * We cannot have an error and a non null *data_ac.
581                  */
582         }
583
584         return ret;
585 }
586
587 static int ocfs2_extend_allocation(struct inode *inode,
588                                    u32 clusters_to_add)
589 {
590         int status = 0;
591         int restart_func = 0;
592         int drop_alloc_sem = 0;
593         int credits;
594         u32 prev_clusters, logical_start;
595         struct buffer_head *bh = NULL;
596         struct ocfs2_dinode *fe = NULL;
597         handle_t *handle = NULL;
598         struct ocfs2_alloc_context *data_ac = NULL;
599         struct ocfs2_alloc_context *meta_ac = NULL;
600         enum ocfs2_alloc_restarted why;
601         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
602
603         mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
604
605         /*
606          * This function only exists for file systems which don't
607          * support holes.
608          */
609         BUG_ON(ocfs2_sparse_alloc(osb));
610
611         status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
612                                   OCFS2_BH_CACHED, inode);
613         if (status < 0) {
614                 mlog_errno(status);
615                 goto leave;
616         }
617
618         fe = (struct ocfs2_dinode *) bh->b_data;
619         if (!OCFS2_IS_VALID_DINODE(fe)) {
620                 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
621                 status = -EIO;
622                 goto leave;
623         }
624
625         logical_start = OCFS2_I(inode)->ip_clusters;
626
627 restart_all:
628         BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
629
630         /* blocks peope in read/write from reading our allocation
631          * until we're done changing it. We depend on i_mutex to block
632          * other extend/truncate calls while we're here. Ordering wrt
633          * start_trans is important here -- always do it before! */
634         down_write(&OCFS2_I(inode)->ip_alloc_sem);
635         drop_alloc_sem = 1;
636
637         status = ocfs2_lock_allocators(inode, fe, clusters_to_add, &data_ac,
638                                        &meta_ac);
639         if (status) {
640                 mlog_errno(status);
641                 goto leave;
642         }
643
644         credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
645         handle = ocfs2_start_trans(osb, credits);
646         if (IS_ERR(handle)) {
647                 status = PTR_ERR(handle);
648                 handle = NULL;
649                 mlog_errno(status);
650                 goto leave;
651         }
652
653 restarted_transaction:
654         /* reserve a write to the file entry early on - that we if we
655          * run out of credits in the allocation path, we can still
656          * update i_size. */
657         status = ocfs2_journal_access(handle, inode, bh,
658                                       OCFS2_JOURNAL_ACCESS_WRITE);
659         if (status < 0) {
660                 mlog_errno(status);
661                 goto leave;
662         }
663
664         prev_clusters = OCFS2_I(inode)->ip_clusters;
665
666         status = ocfs2_do_extend_allocation(osb,
667                                             inode,
668                                             &logical_start,
669                                             clusters_to_add,
670                                             bh,
671                                             handle,
672                                             data_ac,
673                                             meta_ac,
674                                             &why);
675         if ((status < 0) && (status != -EAGAIN)) {
676                 if (status != -ENOSPC)
677                         mlog_errno(status);
678                 goto leave;
679         }
680
681         status = ocfs2_journal_dirty(handle, bh);
682         if (status < 0) {
683                 mlog_errno(status);
684                 goto leave;
685         }
686
687         spin_lock(&OCFS2_I(inode)->ip_lock);
688         clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
689         spin_unlock(&OCFS2_I(inode)->ip_lock);
690
691         if (why != RESTART_NONE && clusters_to_add) {
692                 if (why == RESTART_META) {
693                         mlog(0, "restarting function.\n");
694                         restart_func = 1;
695                 } else {
696                         BUG_ON(why != RESTART_TRANS);
697
698                         mlog(0, "restarting transaction.\n");
699                         /* TODO: This can be more intelligent. */
700                         credits = ocfs2_calc_extend_credits(osb->sb,
701                                                             fe,
702                                                             clusters_to_add);
703                         status = ocfs2_extend_trans(handle, credits);
704                         if (status < 0) {
705                                 /* handle still has to be committed at
706                                  * this point. */
707                                 status = -ENOMEM;
708                                 mlog_errno(status);
709                                 goto leave;
710                         }
711                         goto restarted_transaction;
712                 }
713         }
714
715         mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
716              le32_to_cpu(fe->i_clusters),
717              (unsigned long long)le64_to_cpu(fe->i_size));
718         mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
719              OCFS2_I(inode)->ip_clusters, i_size_read(inode));
720
721 leave:
722         if (drop_alloc_sem) {
723                 up_write(&OCFS2_I(inode)->ip_alloc_sem);
724                 drop_alloc_sem = 0;
725         }
726         if (handle) {
727                 ocfs2_commit_trans(osb, handle);
728                 handle = NULL;
729         }
730         if (data_ac) {
731                 ocfs2_free_alloc_context(data_ac);
732                 data_ac = NULL;
733         }
734         if (meta_ac) {
735                 ocfs2_free_alloc_context(meta_ac);
736                 meta_ac = NULL;
737         }
738         if ((!status) && restart_func) {
739                 restart_func = 0;
740                 goto restart_all;
741         }
742         if (bh) {
743                 brelse(bh);
744                 bh = NULL;
745         }
746
747         mlog_exit(status);
748         return status;
749 }
750
751 /* Some parts of this taken from generic_cont_expand, which turned out
752  * to be too fragile to do exactly what we need without us having to
753  * worry about recursive locking in ->prepare_write() and
754  * ->commit_write(). */
755 static int ocfs2_write_zero_page(struct inode *inode,
756                                  u64 size)
757 {
758         struct address_space *mapping = inode->i_mapping;
759         struct page *page;
760         unsigned long index;
761         unsigned int offset;
762         handle_t *handle = NULL;
763         int ret;
764
765         offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
766         /* ugh.  in prepare/commit_write, if from==to==start of block, we 
767         ** skip the prepare.  make sure we never send an offset for the start
768         ** of a block
769         */
770         if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
771                 offset++;
772         }
773         index = size >> PAGE_CACHE_SHIFT;
774
775         page = grab_cache_page(mapping, index);
776         if (!page) {
777                 ret = -ENOMEM;
778                 mlog_errno(ret);
779                 goto out;
780         }
781
782         ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
783         if (ret < 0) {
784                 mlog_errno(ret);
785                 goto out_unlock;
786         }
787
788         if (ocfs2_should_order_data(inode)) {
789                 handle = ocfs2_start_walk_page_trans(inode, page, offset,
790                                                      offset);
791                 if (IS_ERR(handle)) {
792                         ret = PTR_ERR(handle);
793                         handle = NULL;
794                         goto out_unlock;
795                 }
796         }
797
798         /* must not update i_size! */
799         ret = block_commit_write(page, offset, offset);
800         if (ret < 0)
801                 mlog_errno(ret);
802         else
803                 ret = 0;
804
805         if (handle)
806                 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
807 out_unlock:
808         unlock_page(page);
809         page_cache_release(page);
810 out:
811         return ret;
812 }
813
814 static int ocfs2_zero_extend(struct inode *inode,
815                              u64 zero_to_size)
816 {
817         int ret = 0;
818         u64 start_off;
819         struct super_block *sb = inode->i_sb;
820
821         start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
822         while (start_off < zero_to_size) {
823                 ret = ocfs2_write_zero_page(inode, start_off);
824                 if (ret < 0) {
825                         mlog_errno(ret);
826                         goto out;
827                 }
828
829                 start_off += sb->s_blocksize;
830
831                 /*
832                  * Very large extends have the potential to lock up
833                  * the cpu for extended periods of time.
834                  */
835                 cond_resched();
836         }
837
838 out:
839         return ret;
840 }
841
842 /* 
843  * A tail_to_skip value > 0 indicates that we're being called from
844  * ocfs2_file_aio_write(). This has the following implications:
845  *
846  * - we don't want to update i_size
847  * - di_bh will be NULL, which is fine because it's only used in the
848  *   case where we want to update i_size.
849  * - ocfs2_zero_extend() will then only be filling the hole created
850  *   between i_size and the start of the write.
851  */
852 static int ocfs2_extend_file(struct inode *inode,
853                              struct buffer_head *di_bh,
854                              u64 new_i_size,
855                              size_t tail_to_skip)
856 {
857         int ret = 0;
858         u32 clusters_to_add = 0;
859
860         BUG_ON(!tail_to_skip && !di_bh);
861
862         /* setattr sometimes calls us like this. */
863         if (new_i_size == 0)
864                 goto out;
865
866         if (i_size_read(inode) == new_i_size)
867                 goto out;
868         BUG_ON(new_i_size < i_size_read(inode));
869
870         if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
871                 BUG_ON(tail_to_skip != 0);
872                 goto out_update_size;
873         }
874
875         clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) - 
876                 OCFS2_I(inode)->ip_clusters;
877
878         /* 
879          * protect the pages that ocfs2_zero_extend is going to be
880          * pulling into the page cache.. we do this before the
881          * metadata extend so that we don't get into the situation
882          * where we've extended the metadata but can't get the data
883          * lock to zero.
884          */
885         ret = ocfs2_data_lock(inode, 1);
886         if (ret < 0) {
887                 mlog_errno(ret);
888                 goto out;
889         }
890
891         if (clusters_to_add) {
892                 ret = ocfs2_extend_allocation(inode, clusters_to_add);
893                 if (ret < 0) {
894                         mlog_errno(ret);
895                         goto out_unlock;
896                 }
897         }
898
899         /*
900          * Call this even if we don't add any clusters to the tree. We
901          * still need to zero the area between the old i_size and the
902          * new i_size.
903          */
904         ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
905         if (ret < 0) {
906                 mlog_errno(ret);
907                 goto out_unlock;
908         }
909
910 out_update_size:
911         if (!tail_to_skip) {
912                 /* We're being called from ocfs2_setattr() which wants
913                  * us to update i_size */
914                 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
915                 if (ret < 0)
916                         mlog_errno(ret);
917         }
918
919 out_unlock:
920         if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
921                 ocfs2_data_unlock(inode, 1);
922
923 out:
924         return ret;
925 }
926
927 int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
928 {
929         int status = 0, size_change;
930         struct inode *inode = dentry->d_inode;
931         struct super_block *sb = inode->i_sb;
932         struct ocfs2_super *osb = OCFS2_SB(sb);
933         struct buffer_head *bh = NULL;
934         handle_t *handle = NULL;
935
936         mlog_entry("(0x%p, '%.*s')\n", dentry,
937                    dentry->d_name.len, dentry->d_name.name);
938
939         if (attr->ia_valid & ATTR_MODE)
940                 mlog(0, "mode change: %d\n", attr->ia_mode);
941         if (attr->ia_valid & ATTR_UID)
942                 mlog(0, "uid change: %d\n", attr->ia_uid);
943         if (attr->ia_valid & ATTR_GID)
944                 mlog(0, "gid change: %d\n", attr->ia_gid);
945         if (attr->ia_valid & ATTR_SIZE)
946                 mlog(0, "size change...\n");
947         if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
948                 mlog(0, "time change...\n");
949
950 #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
951                            | ATTR_GID | ATTR_UID | ATTR_MODE)
952         if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
953                 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
954                 return 0;
955         }
956
957         status = inode_change_ok(inode, attr);
958         if (status)
959                 return status;
960
961         size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
962         if (size_change) {
963                 status = ocfs2_rw_lock(inode, 1);
964                 if (status < 0) {
965                         mlog_errno(status);
966                         goto bail;
967                 }
968         }
969
970         status = ocfs2_meta_lock(inode, &bh, 1);
971         if (status < 0) {
972                 if (status != -ENOENT)
973                         mlog_errno(status);
974                 goto bail_unlock_rw;
975         }
976
977         if (size_change && attr->ia_size != i_size_read(inode)) {
978                 if (i_size_read(inode) > attr->ia_size)
979                         status = ocfs2_truncate_file(inode, bh, attr->ia_size);
980                 else
981                         status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
982                 if (status < 0) {
983                         if (status != -ENOSPC)
984                                 mlog_errno(status);
985                         status = -ENOSPC;
986                         goto bail_unlock;
987                 }
988         }
989
990         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
991         if (IS_ERR(handle)) {
992                 status = PTR_ERR(handle);
993                 mlog_errno(status);
994                 goto bail_unlock;
995         }
996
997         status = inode_setattr(inode, attr);
998         if (status < 0) {
999                 mlog_errno(status);
1000                 goto bail_commit;
1001         }
1002
1003         status = ocfs2_mark_inode_dirty(handle, inode, bh);
1004         if (status < 0)
1005                 mlog_errno(status);
1006
1007 bail_commit:
1008         ocfs2_commit_trans(osb, handle);
1009 bail_unlock:
1010         ocfs2_meta_unlock(inode, 1);
1011 bail_unlock_rw:
1012         if (size_change)
1013                 ocfs2_rw_unlock(inode, 1);
1014 bail:
1015         if (bh)
1016                 brelse(bh);
1017
1018         mlog_exit(status);
1019         return status;
1020 }
1021
1022 int ocfs2_getattr(struct vfsmount *mnt,
1023                   struct dentry *dentry,
1024                   struct kstat *stat)
1025 {
1026         struct inode *inode = dentry->d_inode;
1027         struct super_block *sb = dentry->d_inode->i_sb;
1028         struct ocfs2_super *osb = sb->s_fs_info;
1029         int err;
1030
1031         mlog_entry_void();
1032
1033         err = ocfs2_inode_revalidate(dentry);
1034         if (err) {
1035                 if (err != -ENOENT)
1036                         mlog_errno(err);
1037                 goto bail;
1038         }
1039
1040         generic_fillattr(inode, stat);
1041
1042         /* We set the blksize from the cluster size for performance */
1043         stat->blksize = osb->s_clustersize;
1044
1045 bail:
1046         mlog_exit(err);
1047
1048         return err;
1049 }
1050
1051 int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
1052 {
1053         int ret;
1054
1055         mlog_entry_void();
1056
1057         ret = ocfs2_meta_lock(inode, NULL, 0);
1058         if (ret) {
1059                 if (ret != -ENOENT)
1060                         mlog_errno(ret);
1061                 goto out;
1062         }
1063
1064         ret = generic_permission(inode, mask, NULL);
1065
1066         ocfs2_meta_unlock(inode, 0);
1067 out:
1068         mlog_exit(ret);
1069         return ret;
1070 }
1071
1072 static int ocfs2_write_remove_suid(struct inode *inode)
1073 {
1074         int ret;
1075         struct buffer_head *bh = NULL;
1076         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1077         handle_t *handle;
1078         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1079         struct ocfs2_dinode *di;
1080
1081         mlog_entry("(Inode %llu, mode 0%o)\n",
1082                    (unsigned long long)oi->ip_blkno, inode->i_mode);
1083
1084         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1085         if (handle == NULL) {
1086                 ret = -ENOMEM;
1087                 mlog_errno(ret);
1088                 goto out;
1089         }
1090
1091         ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
1092         if (ret < 0) {
1093                 mlog_errno(ret);
1094                 goto out_trans;
1095         }
1096
1097         ret = ocfs2_journal_access(handle, inode, bh,
1098                                    OCFS2_JOURNAL_ACCESS_WRITE);
1099         if (ret < 0) {
1100                 mlog_errno(ret);
1101                 goto out_bh;
1102         }
1103
1104         inode->i_mode &= ~S_ISUID;
1105         if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1106                 inode->i_mode &= ~S_ISGID;
1107
1108         di = (struct ocfs2_dinode *) bh->b_data;
1109         di->i_mode = cpu_to_le16(inode->i_mode);
1110
1111         ret = ocfs2_journal_dirty(handle, bh);
1112         if (ret < 0)
1113                 mlog_errno(ret);
1114 out_bh:
1115         brelse(bh);
1116 out_trans:
1117         ocfs2_commit_trans(osb, handle);
1118 out:
1119         mlog_exit(ret);
1120         return ret;
1121 }
1122
1123 /*
1124  * Will look for holes and unwritten extents in the range starting at
1125  * pos for count bytes (inclusive).
1126  */
1127 static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1128                                        size_t count)
1129 {
1130         int ret = 0;
1131         unsigned int extent_flags;
1132         u32 cpos, clusters, extent_len, phys_cpos;
1133         struct super_block *sb = inode->i_sb;
1134
1135         cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
1136         clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
1137
1138         while (clusters) {
1139                 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
1140                                          &extent_flags);
1141                 if (ret < 0) {
1142                         mlog_errno(ret);
1143                         goto out;
1144                 }
1145
1146                 if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
1147                         ret = 1;
1148                         break;
1149                 }
1150
1151                 if (extent_len > clusters)
1152                         extent_len = clusters;
1153
1154                 clusters -= extent_len;
1155                 cpos += extent_len;
1156         }
1157 out:
1158         return ret;
1159 }
1160
1161 static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1162                                          loff_t *ppos,
1163                                          size_t count,
1164                                          int appending,
1165                                          int *direct_io)
1166 {
1167         int ret = 0, meta_level = appending;
1168         struct inode *inode = dentry->d_inode;
1169         u32 clusters;
1170         loff_t newsize, saved_pos;
1171
1172         /* 
1173          * We sample i_size under a read level meta lock to see if our write
1174          * is extending the file, if it is we back off and get a write level
1175          * meta lock.
1176          */
1177         for(;;) {
1178                 ret = ocfs2_meta_lock(inode, NULL, meta_level);
1179                 if (ret < 0) {
1180                         meta_level = -1;
1181                         mlog_errno(ret);
1182                         goto out;
1183                 }
1184
1185                 /* Clear suid / sgid if necessary. We do this here
1186                  * instead of later in the write path because
1187                  * remove_suid() calls ->setattr without any hint that
1188                  * we may have already done our cluster locking. Since
1189                  * ocfs2_setattr() *must* take cluster locks to
1190                  * proceeed, this will lead us to recursively lock the
1191                  * inode. There's also the dinode i_size state which
1192                  * can be lost via setattr during extending writes (we
1193                  * set inode->i_size at the end of a write. */
1194                 if (should_remove_suid(dentry)) {
1195                         if (meta_level == 0) {
1196                                 ocfs2_meta_unlock(inode, meta_level);
1197                                 meta_level = 1;
1198                                 continue;
1199                         }
1200
1201                         ret = ocfs2_write_remove_suid(inode);
1202                         if (ret < 0) {
1203                                 mlog_errno(ret);
1204                                 goto out_unlock;
1205                         }
1206                 }
1207
1208                 /* work on a copy of ppos until we're sure that we won't have
1209                  * to recalculate it due to relocking. */
1210                 if (appending) {
1211                         saved_pos = i_size_read(inode);
1212                         mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1213                 } else {
1214                         saved_pos = *ppos;
1215                 }
1216
1217                 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
1218                         loff_t end = saved_pos + count;
1219
1220                         /*
1221                          * Skip the O_DIRECT checks if we don't need
1222                          * them.
1223                          */
1224                         if (!direct_io || !(*direct_io))
1225                                 break;
1226
1227                         /*
1228                          * Allowing concurrent direct writes means
1229                          * i_size changes wouldn't be synchronized, so
1230                          * one node could wind up truncating another
1231                          * nodes writes.
1232                          */
1233                         if (end > i_size_read(inode)) {
1234                                 *direct_io = 0;
1235                                 break;
1236                         }
1237
1238                         /*
1239                          * We don't fill holes during direct io, so
1240                          * check for them here. If any are found, the
1241                          * caller will have to retake some cluster
1242                          * locks and initiate the io as buffered.
1243                          */
1244                         ret = ocfs2_check_range_for_holes(inode, saved_pos,
1245                                                           count);
1246                         if (ret == 1) {
1247                                 *direct_io = 0;
1248                                 ret = 0;
1249                         } else if (ret < 0)
1250                                 mlog_errno(ret);
1251                         break;
1252                 }
1253
1254                 /*
1255                  * The rest of this loop is concerned with legacy file
1256                  * systems which don't support sparse files.
1257                  */
1258
1259                 newsize = count + saved_pos;
1260
1261                 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1262                      (long long) saved_pos, (long long) newsize,
1263                      (long long) i_size_read(inode));
1264
1265                 /* No need for a higher level metadata lock if we're
1266                  * never going past i_size. */
1267                 if (newsize <= i_size_read(inode))
1268                         break;
1269
1270                 if (meta_level == 0) {
1271                         ocfs2_meta_unlock(inode, meta_level);
1272                         meta_level = 1;
1273                         continue;
1274                 }
1275
1276                 spin_lock(&OCFS2_I(inode)->ip_lock);
1277                 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1278                         OCFS2_I(inode)->ip_clusters;
1279                 spin_unlock(&OCFS2_I(inode)->ip_lock);
1280
1281                 mlog(0, "Writing at EOF, may need more allocation: "
1282                      "i_size = %lld, newsize = %lld, need %u clusters\n",
1283                      (long long) i_size_read(inode), (long long) newsize,
1284                      clusters);
1285
1286                 /* We only want to continue the rest of this loop if
1287                  * our extend will actually require more
1288                  * allocation. */
1289                 if (!clusters)
1290                         break;
1291
1292                 ret = ocfs2_extend_file(inode, NULL, newsize, count);
1293                 if (ret < 0) {
1294                         if (ret != -ENOSPC)
1295                                 mlog_errno(ret);
1296                         goto out_unlock;
1297                 }
1298                 break;
1299         }
1300
1301         if (appending)
1302                 *ppos = saved_pos;
1303
1304 out_unlock:
1305         ocfs2_meta_unlock(inode, meta_level);
1306
1307 out:
1308         return ret;
1309 }
1310
1311 static inline void
1312 ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1313 {
1314         const struct iovec *iov = *iovp;
1315         size_t base = *basep;
1316
1317         do {
1318                 int copy = min(bytes, iov->iov_len - base);
1319
1320                 bytes -= copy;
1321                 base += copy;
1322                 if (iov->iov_len == base) {
1323                         iov++;
1324                         base = 0;
1325                 }
1326         } while (bytes);
1327         *iovp = iov;
1328         *basep = base;
1329 }
1330
1331 static struct page * ocfs2_get_write_source(struct ocfs2_buffered_write_priv *bp,
1332                                             const struct iovec *cur_iov,
1333                                             size_t iov_offset)
1334 {
1335         int ret;
1336         char *buf;
1337         struct page *src_page = NULL;
1338
1339         buf = cur_iov->iov_base + iov_offset;
1340
1341         if (!segment_eq(get_fs(), KERNEL_DS)) {
1342                 /*
1343                  * Pull in the user page. We want to do this outside
1344                  * of the meta data locks in order to preserve locking
1345                  * order in case of page fault.
1346                  */
1347                 ret = get_user_pages(current, current->mm,
1348                                      (unsigned long)buf & PAGE_CACHE_MASK, 1,
1349                                      0, 0, &src_page, NULL);
1350                 if (ret == 1)
1351                         bp->b_src_buf = kmap(src_page);
1352                 else
1353                         src_page = ERR_PTR(-EFAULT);
1354         } else {
1355                 bp->b_src_buf = buf;
1356         }
1357
1358         return src_page;
1359 }
1360
1361 static void ocfs2_put_write_source(struct ocfs2_buffered_write_priv *bp,
1362                                    struct page *page)
1363 {
1364         if (page) {
1365                 kunmap(page);
1366                 page_cache_release(page);
1367         }
1368 }
1369
1370 static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
1371                                          const struct iovec *iov,
1372                                          unsigned long nr_segs,
1373                                          size_t count,
1374                                          ssize_t o_direct_written)
1375 {
1376         int ret = 0;
1377         ssize_t copied, total = 0;
1378         size_t iov_offset = 0;
1379         const struct iovec *cur_iov = iov;
1380         struct ocfs2_buffered_write_priv bp;
1381         struct page *page;
1382
1383         /*
1384          * handle partial DIO write.  Adjust cur_iov if needed.
1385          */
1386         ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
1387
1388         do {
1389                 bp.b_cur_off = iov_offset;
1390                 bp.b_cur_iov = cur_iov;
1391
1392                 page = ocfs2_get_write_source(&bp, cur_iov, iov_offset);
1393                 if (IS_ERR(page)) {
1394                         ret = PTR_ERR(page);
1395                         goto out;
1396                 }
1397
1398                 copied = ocfs2_buffered_write_cluster(file, *ppos, count,
1399                                                       ocfs2_map_and_write_user_data,
1400                                                       &bp);
1401
1402                 ocfs2_put_write_source(&bp, page);
1403
1404                 if (copied < 0) {
1405                         mlog_errno(copied);
1406                         ret = copied;
1407                         goto out;
1408                 }
1409
1410                 total += copied;
1411                 *ppos = *ppos + copied;
1412                 count -= copied;
1413
1414                 ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
1415         } while(count);
1416
1417 out:
1418         return total ? total : ret;
1419 }
1420
1421 static int ocfs2_check_iovec(const struct iovec *iov, size_t *counted,
1422                              unsigned long *nr_segs)
1423 {
1424         size_t ocount;          /* original count */
1425         unsigned long seg;
1426
1427         ocount = 0;
1428         for (seg = 0; seg < *nr_segs; seg++) {
1429                 const struct iovec *iv = &iov[seg];
1430
1431                 /*
1432                  * If any segment has a negative length, or the cumulative
1433                  * length ever wraps negative then return -EINVAL.
1434                  */
1435                 ocount += iv->iov_len;
1436                 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
1437                         return -EINVAL;
1438                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
1439                         continue;
1440                 if (seg == 0)
1441                         return -EFAULT;
1442                 *nr_segs = seg;
1443                 ocount -= iv->iov_len;  /* This segment is no good */
1444                 break;
1445         }
1446
1447         *counted = ocount;
1448         return 0;
1449 }
1450
1451 static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
1452                                     const struct iovec *iov,
1453                                     unsigned long nr_segs,
1454                                     loff_t pos)
1455 {
1456         int ret, direct_io, appending, rw_level, have_alloc_sem  = 0;
1457         int can_do_direct, sync = 0;
1458         ssize_t written = 0;
1459         size_t ocount;          /* original count */
1460         size_t count;           /* after file limit checks */
1461         loff_t *ppos = &iocb->ki_pos;
1462         struct file *file = iocb->ki_filp;
1463         struct inode *inode = file->f_path.dentry->d_inode;
1464
1465         mlog_entry("(0x%p, %u, '%.*s')\n", file,
1466                    (unsigned int)nr_segs,
1467                    file->f_path.dentry->d_name.len,
1468                    file->f_path.dentry->d_name.name);
1469
1470         if (iocb->ki_left == 0)
1471                 return 0;
1472
1473         ret = ocfs2_check_iovec(iov, &ocount, &nr_segs);
1474         if (ret)
1475                 return ret;
1476
1477         count = ocount;
1478
1479         vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1480
1481         appending = file->f_flags & O_APPEND ? 1 : 0;
1482         direct_io = file->f_flags & O_DIRECT ? 1 : 0;
1483
1484         mutex_lock(&inode->i_mutex);
1485
1486 relock:
1487         /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
1488         if (direct_io) {
1489                 down_read(&inode->i_alloc_sem);
1490                 have_alloc_sem = 1;
1491         }
1492
1493         /* concurrent O_DIRECT writes are allowed */
1494         rw_level = !direct_io;
1495         ret = ocfs2_rw_lock(inode, rw_level);
1496         if (ret < 0) {
1497                 mlog_errno(ret);
1498                 goto out_sems;
1499         }
1500
1501         can_do_direct = direct_io;
1502         ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
1503                                             iocb->ki_left, appending,
1504                                             &can_do_direct);
1505         if (ret < 0) {
1506                 mlog_errno(ret);
1507                 goto out;
1508         }
1509
1510         /*
1511          * We can't complete the direct I/O as requested, fall back to
1512          * buffered I/O.
1513          */
1514         if (direct_io && !can_do_direct) {
1515                 ocfs2_rw_unlock(inode, rw_level);
1516                 up_read(&inode->i_alloc_sem);
1517
1518                 have_alloc_sem = 0;
1519                 rw_level = -1;
1520
1521                 direct_io = 0;
1522                 sync = 1;
1523                 goto relock;
1524         }
1525
1526         if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
1527                 sync = 1;
1528
1529         /*
1530          * XXX: Is it ok to execute these checks a second time?
1531          */
1532         ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
1533         if (ret)
1534                 goto out;
1535
1536         /*
1537          * Set pos so that sync_page_range_nolock() below understands
1538          * where to start from. We might've moved it around via the
1539          * calls above. The range we want to actually sync starts from
1540          * *ppos here.
1541          *
1542          */
1543         pos = *ppos;
1544
1545         /* communicate with ocfs2_dio_end_io */
1546         ocfs2_iocb_set_rw_locked(iocb, rw_level);
1547
1548         if (direct_io) {
1549                 written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
1550                                                     ppos, count, ocount);
1551                 if (written < 0) {
1552                         ret = written;
1553                         goto out_dio;
1554                 }
1555         } else {
1556                 written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
1557                                                     count, written);
1558                 if (written < 0) {
1559                         ret = written;
1560                         if (ret != -EFAULT || ret != -ENOSPC)
1561                                 mlog_errno(ret);
1562                         goto out;
1563                 }
1564         }
1565
1566 out_dio:
1567         /* buffered aio wouldn't have proper lock coverage today */
1568         BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
1569
1570         /* 
1571          * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1572          * function pointer which is called when o_direct io completes so that
1573          * it can unlock our rw lock.  (it's the clustered equivalent of
1574          * i_alloc_sem; protects truncate from racing with pending ios).
1575          * Unfortunately there are error cases which call end_io and others
1576          * that don't.  so we don't have to unlock the rw_lock if either an
1577          * async dio is going to do it in the future or an end_io after an
1578          * error has already done it.
1579          */
1580         if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1581                 rw_level = -1;
1582                 have_alloc_sem = 0;
1583         }
1584
1585 out:
1586         if (rw_level != -1)
1587                 ocfs2_rw_unlock(inode, rw_level);
1588
1589 out_sems:
1590         if (have_alloc_sem)
1591                 up_read(&inode->i_alloc_sem);
1592
1593         if (written > 0 && sync) {
1594                 ssize_t err;
1595
1596                 err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
1597                 if (err < 0)
1598                         written = err;
1599         }
1600
1601         mutex_unlock(&inode->i_mutex);
1602
1603         mlog_exit(ret);
1604         return written ? written : ret;
1605 }
1606
1607 static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
1608                                     struct pipe_buffer *buf,
1609                                     struct splice_desc *sd)
1610 {
1611         int ret, count, total = 0;
1612         ssize_t copied = 0;
1613         struct ocfs2_splice_write_priv sp;
1614
1615         ret = buf->ops->pin(pipe, buf);
1616         if (ret)
1617                 goto out;
1618
1619         sp.s_sd = sd;
1620         sp.s_buf = buf;
1621         sp.s_pipe = pipe;
1622         sp.s_offset = sd->pos & ~PAGE_CACHE_MASK;
1623         sp.s_buf_offset = buf->offset;
1624
1625         count = sd->len;
1626         if (count + sp.s_offset > PAGE_CACHE_SIZE)
1627                 count = PAGE_CACHE_SIZE - sp.s_offset;
1628
1629         do {
1630                 /*
1631                  * splice wants us to copy up to one page at a
1632                  * time. For pagesize > cluster size, this means we
1633                  * might enter ocfs2_buffered_write_cluster() more
1634                  * than once, so keep track of our progress here.
1635                  */
1636                 copied = ocfs2_buffered_write_cluster(sd->file,
1637                                                       (loff_t)sd->pos + total,
1638                                                       count,
1639                                                       ocfs2_map_and_write_splice_data,
1640                                                       &sp);
1641                 if (copied < 0) {
1642                         mlog_errno(copied);
1643                         ret = copied;
1644                         goto out;
1645                 }
1646
1647                 count -= copied;
1648                 sp.s_offset += copied;
1649                 sp.s_buf_offset += copied;
1650                 total += copied;
1651         } while (count);
1652
1653         ret = 0;
1654 out:
1655
1656         return total ? total : ret;
1657 }
1658
1659 static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1660                                          struct file *out,
1661                                          loff_t *ppos,
1662                                          size_t len,
1663                                          unsigned int flags)
1664 {
1665         int ret, err;
1666         struct address_space *mapping = out->f_mapping;
1667         struct inode *inode = mapping->host;
1668
1669         ret = __splice_from_pipe(pipe, out, ppos, len, flags,
1670                                  ocfs2_splice_write_actor);
1671         if (ret > 0) {
1672                 *ppos += ret;
1673
1674                 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
1675                         err = generic_osync_inode(inode, mapping,
1676                                                   OSYNC_METADATA|OSYNC_DATA);
1677                         if (err)
1678                                 ret = err;
1679                 }
1680         }
1681
1682         return ret;
1683 }
1684
1685 static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
1686                                        struct file *out,
1687                                        loff_t *ppos,
1688                                        size_t len,
1689                                        unsigned int flags)
1690 {
1691         int ret;
1692         struct inode *inode = out->f_path.dentry->d_inode;
1693
1694         mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
1695                    (unsigned int)len,
1696                    out->f_path.dentry->d_name.len,
1697                    out->f_path.dentry->d_name.name);
1698
1699         inode_double_lock(inode, pipe->inode);
1700
1701         ret = ocfs2_rw_lock(inode, 1);
1702         if (ret < 0) {
1703                 mlog_errno(ret);
1704                 goto out;
1705         }
1706
1707         ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
1708                                             NULL);
1709         if (ret < 0) {
1710                 mlog_errno(ret);
1711                 goto out_unlock;
1712         }
1713
1714         /* ok, we're done with i_size and alloc work */
1715         ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
1716
1717 out_unlock:
1718         ocfs2_rw_unlock(inode, 1);
1719 out:
1720         inode_double_unlock(inode, pipe->inode);
1721
1722         mlog_exit(ret);
1723         return ret;
1724 }
1725
1726 static ssize_t ocfs2_file_splice_read(struct file *in,
1727                                       loff_t *ppos,
1728                                       struct pipe_inode_info *pipe,
1729                                       size_t len,
1730                                       unsigned int flags)
1731 {
1732         int ret = 0;
1733         struct inode *inode = in->f_path.dentry->d_inode;
1734
1735         mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
1736                    (unsigned int)len,
1737                    in->f_path.dentry->d_name.len,
1738                    in->f_path.dentry->d_name.name);
1739
1740         /*
1741          * See the comment in ocfs2_file_aio_read()
1742          */
1743         ret = ocfs2_meta_lock(inode, NULL, 0);
1744         if (ret < 0) {
1745                 mlog_errno(ret);
1746                 goto bail;
1747         }
1748         ocfs2_meta_unlock(inode, 0);
1749
1750         ret = generic_file_splice_read(in, ppos, pipe, len, flags);
1751
1752 bail:
1753         mlog_exit(ret);
1754         return ret;
1755 }
1756
1757 static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
1758                                    const struct iovec *iov,
1759                                    unsigned long nr_segs,
1760                                    loff_t pos)
1761 {
1762         int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
1763         struct file *filp = iocb->ki_filp;
1764         struct inode *inode = filp->f_path.dentry->d_inode;
1765
1766         mlog_entry("(0x%p, %u, '%.*s')\n", filp,
1767                    (unsigned int)nr_segs,
1768                    filp->f_path.dentry->d_name.len,
1769                    filp->f_path.dentry->d_name.name);
1770
1771         if (!inode) {
1772                 ret = -EINVAL;
1773                 mlog_errno(ret);
1774                 goto bail;
1775         }
1776
1777         /* 
1778          * buffered reads protect themselves in ->readpage().  O_DIRECT reads
1779          * need locks to protect pending reads from racing with truncate.
1780          */
1781         if (filp->f_flags & O_DIRECT) {
1782                 down_read(&inode->i_alloc_sem);
1783                 have_alloc_sem = 1;
1784
1785                 ret = ocfs2_rw_lock(inode, 0);
1786                 if (ret < 0) {
1787                         mlog_errno(ret);
1788                         goto bail;
1789                 }
1790                 rw_level = 0;
1791                 /* communicate with ocfs2_dio_end_io */
1792                 ocfs2_iocb_set_rw_locked(iocb, rw_level);
1793         }
1794
1795         /*
1796          * We're fine letting folks race truncates and extending
1797          * writes with read across the cluster, just like they can
1798          * locally. Hence no rw_lock during read.
1799          * 
1800          * Take and drop the meta data lock to update inode fields
1801          * like i_size. This allows the checks down below
1802          * generic_file_aio_read() a chance of actually working. 
1803          */
1804         ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
1805         if (ret < 0) {
1806                 mlog_errno(ret);
1807                 goto bail;
1808         }
1809         ocfs2_meta_unlock(inode, lock_level);
1810
1811         ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
1812         if (ret == -EINVAL)
1813                 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
1814
1815         /* buffered aio wouldn't have proper lock coverage today */
1816         BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1817
1818         /* see ocfs2_file_aio_write */
1819         if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1820                 rw_level = -1;
1821                 have_alloc_sem = 0;
1822         }
1823
1824 bail:
1825         if (have_alloc_sem)
1826                 up_read(&inode->i_alloc_sem);
1827         if (rw_level != -1) 
1828                 ocfs2_rw_unlock(inode, rw_level);
1829         mlog_exit(ret);
1830
1831         return ret;
1832 }
1833
1834 const struct inode_operations ocfs2_file_iops = {
1835         .setattr        = ocfs2_setattr,
1836         .getattr        = ocfs2_getattr,
1837         .permission     = ocfs2_permission,
1838 };
1839
1840 const struct inode_operations ocfs2_special_file_iops = {
1841         .setattr        = ocfs2_setattr,
1842         .getattr        = ocfs2_getattr,
1843         .permission     = ocfs2_permission,
1844 };
1845
1846 const struct file_operations ocfs2_fops = {
1847         .read           = do_sync_read,
1848         .write          = do_sync_write,
1849         .sendfile       = generic_file_sendfile,
1850         .mmap           = ocfs2_mmap,
1851         .fsync          = ocfs2_sync_file,
1852         .release        = ocfs2_file_release,
1853         .open           = ocfs2_file_open,
1854         .aio_read       = ocfs2_file_aio_read,
1855         .aio_write      = ocfs2_file_aio_write,
1856         .ioctl          = ocfs2_ioctl,
1857 #ifdef CONFIG_COMPAT
1858         .compat_ioctl   = ocfs2_compat_ioctl,
1859 #endif
1860         .splice_read    = ocfs2_file_splice_read,
1861         .splice_write   = ocfs2_file_splice_write,
1862 };
1863
1864 const struct file_operations ocfs2_dops = {
1865         .read           = generic_read_dir,
1866         .readdir        = ocfs2_readdir,
1867         .fsync          = ocfs2_sync_file,
1868         .ioctl          = ocfs2_ioctl,
1869 #ifdef CONFIG_COMPAT
1870         .compat_ioctl   = ocfs2_compat_ioctl,
1871 #endif
1872 };