Merge branch 'upstream-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/linvil...
[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
35 #define MLOG_MASK_PREFIX ML_INODE
36 #include <cluster/masklog.h>
37
38 #include "ocfs2.h"
39
40 #include "alloc.h"
41 #include "aops.h"
42 #include "dir.h"
43 #include "dlmglue.h"
44 #include "extent_map.h"
45 #include "file.h"
46 #include "sysfile.h"
47 #include "inode.h"
48 #include "ioctl.h"
49 #include "journal.h"
50 #include "mmap.h"
51 #include "suballoc.h"
52 #include "super.h"
53
54 #include "buffer_head_io.h"
55
56 static int ocfs2_sync_inode(struct inode *inode)
57 {
58         filemap_fdatawrite(inode->i_mapping);
59         return sync_mapping_buffers(inode->i_mapping);
60 }
61
62 static int ocfs2_file_open(struct inode *inode, struct file *file)
63 {
64         int status;
65         int mode = file->f_flags;
66         struct ocfs2_inode_info *oi = OCFS2_I(inode);
67
68         mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
69                    file->f_dentry->d_name.len, file->f_dentry->d_name.name);
70
71         spin_lock(&oi->ip_lock);
72
73         /* Check that the inode hasn't been wiped from disk by another
74          * node. If it hasn't then we're safe as long as we hold the
75          * spin lock until our increment of open count. */
76         if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
77                 spin_unlock(&oi->ip_lock);
78
79                 status = -ENOENT;
80                 goto leave;
81         }
82
83         if (mode & O_DIRECT)
84                 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
85
86         oi->ip_open_count++;
87         spin_unlock(&oi->ip_lock);
88         status = 0;
89 leave:
90         mlog_exit(status);
91         return status;
92 }
93
94 static int ocfs2_file_release(struct inode *inode, struct file *file)
95 {
96         struct ocfs2_inode_info *oi = OCFS2_I(inode);
97
98         mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
99                        file->f_dentry->d_name.len,
100                        file->f_dentry->d_name.name);
101
102         spin_lock(&oi->ip_lock);
103         if (!--oi->ip_open_count)
104                 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
105         spin_unlock(&oi->ip_lock);
106
107         mlog_exit(0);
108
109         return 0;
110 }
111
112 static int ocfs2_sync_file(struct file *file,
113                            struct dentry *dentry,
114                            int datasync)
115 {
116         int err = 0;
117         journal_t *journal;
118         struct inode *inode = dentry->d_inode;
119         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
120
121         mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
122                    dentry->d_name.len, dentry->d_name.name);
123
124         err = ocfs2_sync_inode(dentry->d_inode);
125         if (err)
126                 goto bail;
127
128         journal = osb->journal->j_journal;
129         err = journal_force_commit(journal);
130
131 bail:
132         mlog_exit(err);
133
134         return (err < 0) ? -EIO : 0;
135 }
136
137 int ocfs2_set_inode_size(struct ocfs2_journal_handle *handle,
138                          struct inode *inode,
139                          struct buffer_head *fe_bh,
140                          u64 new_i_size)
141 {
142         int status;
143
144         mlog_entry_void();
145         i_size_write(inode, new_i_size);
146         inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
147         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
148
149         status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
150         if (status < 0) {
151                 mlog_errno(status);
152                 goto bail;
153         }
154
155 bail:
156         mlog_exit(status);
157         return status;
158 }
159
160 static int ocfs2_simple_size_update(struct inode *inode,
161                                     struct buffer_head *di_bh,
162                                     u64 new_i_size)
163 {
164         int ret;
165         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
166         struct ocfs2_journal_handle *handle = NULL;
167
168         handle = ocfs2_start_trans(osb, NULL,
169                                    OCFS2_INODE_UPDATE_CREDITS);
170         if (handle == NULL) {
171                 ret = -ENOMEM;
172                 mlog_errno(ret);
173                 goto out;
174         }
175
176         ret = ocfs2_set_inode_size(handle, inode, di_bh,
177                                    new_i_size);
178         if (ret < 0)
179                 mlog_errno(ret);
180
181         ocfs2_commit_trans(handle);
182 out:
183         return ret;
184 }
185
186 static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
187                                      struct inode *inode,
188                                      struct buffer_head *fe_bh,
189                                      u64 new_i_size)
190 {
191         int status;
192         struct ocfs2_journal_handle *handle;
193
194         mlog_entry_void();
195
196         /* TODO: This needs to actually orphan the inode in this
197          * transaction. */
198
199         handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
200         if (IS_ERR(handle)) {
201                 status = PTR_ERR(handle);
202                 mlog_errno(status);
203                 goto out;
204         }
205
206         status = ocfs2_set_inode_size(handle, inode, fe_bh, new_i_size);
207         if (status < 0)
208                 mlog_errno(status);
209
210         ocfs2_commit_trans(handle);
211 out:
212         mlog_exit(status);
213         return status;
214 }
215
216 static int ocfs2_truncate_file(struct inode *inode,
217                                struct buffer_head *di_bh,
218                                u64 new_i_size)
219 {
220         int status = 0;
221         struct ocfs2_dinode *fe = NULL;
222         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
223         struct ocfs2_truncate_context *tc = NULL;
224
225         mlog_entry("(inode = %llu, new_i_size = %llu\n",
226                    (unsigned long long)OCFS2_I(inode)->ip_blkno,
227                    (unsigned long long)new_i_size);
228
229         truncate_inode_pages(inode->i_mapping, new_i_size);
230
231         fe = (struct ocfs2_dinode *) di_bh->b_data;
232         if (!OCFS2_IS_VALID_DINODE(fe)) {
233                 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
234                 status = -EIO;
235                 goto bail;
236         }
237
238         mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
239                         "Inode %llu, inode i_size = %lld != di "
240                         "i_size = %llu, i_flags = 0x%x\n",
241                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
242                         i_size_read(inode),
243                         (unsigned long long)le64_to_cpu(fe->i_size),
244                         le32_to_cpu(fe->i_flags));
245
246         if (new_i_size > le64_to_cpu(fe->i_size)) {
247                 mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
248                      (unsigned long long)le64_to_cpu(fe->i_size),
249                      (unsigned long long)new_i_size);
250                 status = -EINVAL;
251                 mlog_errno(status);
252                 goto bail;
253         }
254
255         mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
256              (unsigned long long)le64_to_cpu(fe->i_blkno),
257              (unsigned long long)le64_to_cpu(fe->i_size),
258              (unsigned long long)new_i_size);
259
260         /* lets handle the simple truncate cases before doing any more
261          * cluster locking. */
262         if (new_i_size == le64_to_cpu(fe->i_size))
263                 goto bail;
264
265         /* This forces other nodes to sync and drop their pages. Do
266          * this even if we have a truncate without allocation change -
267          * ocfs2 cluster sizes can be much greater than page size, so
268          * we have to truncate them anyway.  */
269         status = ocfs2_data_lock(inode, 1);
270         if (status < 0) {
271                 mlog_errno(status);
272                 goto bail;
273         }
274         ocfs2_data_unlock(inode, 1);
275
276         if (le32_to_cpu(fe->i_clusters) ==
277             ocfs2_clusters_for_bytes(osb->sb, new_i_size)) {
278                 mlog(0, "fe->i_clusters = %u, so we do a simple truncate\n",
279                      fe->i_clusters);
280                 /* No allocation change is required, so lets fast path
281                  * this truncate. */
282                 status = ocfs2_simple_size_update(inode, di_bh, new_i_size);
283                 if (status < 0)
284                         mlog_errno(status);
285                 goto bail;
286         }
287
288         /* alright, we're going to need to do a full blown alloc size
289          * change. Orphan the inode so that recovery can complete the
290          * truncate if necessary. This does the task of marking
291          * i_size. */
292         status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
293         if (status < 0) {
294                 mlog_errno(status);
295                 goto bail;
296         }
297
298         status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
299         if (status < 0) {
300                 mlog_errno(status);
301                 goto bail;
302         }
303
304         status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
305         if (status < 0) {
306                 mlog_errno(status);
307                 goto bail;
308         }
309
310         /* TODO: orphan dir cleanup here. */
311 bail:
312
313         mlog_exit(status);
314         return status;
315 }
316
317 /*
318  * extend allocation only here.
319  * we'll update all the disk stuff, and oip->alloc_size
320  *
321  * expect stuff to be locked, a transaction started and enough data /
322  * metadata reservations in the contexts.
323  *
324  * Will return -EAGAIN, and a reason if a restart is needed.
325  * If passed in, *reason will always be set, even in error.
326  */
327 int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
328                                struct inode *inode,
329                                u32 clusters_to_add,
330                                struct buffer_head *fe_bh,
331                                struct ocfs2_journal_handle *handle,
332                                struct ocfs2_alloc_context *data_ac,
333                                struct ocfs2_alloc_context *meta_ac,
334                                enum ocfs2_alloc_restarted *reason_ret)
335 {
336         int status = 0;
337         int free_extents;
338         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
339         enum ocfs2_alloc_restarted reason = RESTART_NONE;
340         u32 bit_off, num_bits;
341         u64 block;
342
343         BUG_ON(!clusters_to_add);
344
345         free_extents = ocfs2_num_free_extents(osb, inode, fe);
346         if (free_extents < 0) {
347                 status = free_extents;
348                 mlog_errno(status);
349                 goto leave;
350         }
351
352         /* there are two cases which could cause us to EAGAIN in the
353          * we-need-more-metadata case:
354          * 1) we haven't reserved *any*
355          * 2) we are so fragmented, we've needed to add metadata too
356          *    many times. */
357         if (!free_extents && !meta_ac) {
358                 mlog(0, "we haven't reserved any metadata!\n");
359                 status = -EAGAIN;
360                 reason = RESTART_META;
361                 goto leave;
362         } else if ((!free_extents)
363                    && (ocfs2_alloc_context_bits_left(meta_ac)
364                        < ocfs2_extend_meta_needed(fe))) {
365                 mlog(0, "filesystem is really fragmented...\n");
366                 status = -EAGAIN;
367                 reason = RESTART_META;
368                 goto leave;
369         }
370
371         status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
372                                       &bit_off, &num_bits);
373         if (status < 0) {
374                 if (status != -ENOSPC)
375                         mlog_errno(status);
376                 goto leave;
377         }
378
379         BUG_ON(num_bits > clusters_to_add);
380
381         /* reserve our write early -- insert_extent may update the inode */
382         status = ocfs2_journal_access(handle, inode, fe_bh,
383                                       OCFS2_JOURNAL_ACCESS_WRITE);
384         if (status < 0) {
385                 mlog_errno(status);
386                 goto leave;
387         }
388
389         block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
390         mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
391              num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
392         status = ocfs2_insert_extent(osb, handle, inode, fe_bh, block,
393                                      num_bits, meta_ac);
394         if (status < 0) {
395                 mlog_errno(status);
396                 goto leave;
397         }
398
399         le32_add_cpu(&fe->i_clusters, num_bits);
400         spin_lock(&OCFS2_I(inode)->ip_lock);
401         OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
402         spin_unlock(&OCFS2_I(inode)->ip_lock);
403
404         status = ocfs2_journal_dirty(handle, fe_bh);
405         if (status < 0) {
406                 mlog_errno(status);
407                 goto leave;
408         }
409
410         clusters_to_add -= num_bits;
411
412         if (clusters_to_add) {
413                 mlog(0, "need to alloc once more, clusters = %u, wanted = "
414                      "%u\n", fe->i_clusters, clusters_to_add);
415                 status = -EAGAIN;
416                 reason = RESTART_TRANS;
417         }
418
419 leave:
420         mlog_exit(status);
421         if (reason_ret)
422                 *reason_ret = reason;
423         return status;
424 }
425
426 static int ocfs2_extend_allocation(struct inode *inode,
427                                    u32 clusters_to_add)
428 {
429         int status = 0;
430         int restart_func = 0;
431         int drop_alloc_sem = 0;
432         int credits, num_free_extents;
433         u32 prev_clusters;
434         struct buffer_head *bh = NULL;
435         struct ocfs2_dinode *fe = NULL;
436         struct ocfs2_journal_handle *handle = NULL;
437         struct ocfs2_alloc_context *data_ac = NULL;
438         struct ocfs2_alloc_context *meta_ac = NULL;
439         enum ocfs2_alloc_restarted why;
440         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
441
442         mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
443
444         status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
445                                   OCFS2_BH_CACHED, inode);
446         if (status < 0) {
447                 mlog_errno(status);
448                 goto leave;
449         }
450
451         fe = (struct ocfs2_dinode *) bh->b_data;
452         if (!OCFS2_IS_VALID_DINODE(fe)) {
453                 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
454                 status = -EIO;
455                 goto leave;
456         }
457
458 restart_all:
459         BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
460
461         mlog(0, "extend inode %llu, i_size = %lld, fe->i_clusters = %u, "
462              "clusters_to_add = %u\n",
463              (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
464              fe->i_clusters, clusters_to_add);
465
466         handle = ocfs2_alloc_handle(osb);
467         if (handle == NULL) {
468                 status = -ENOMEM;
469                 mlog_errno(status);
470                 goto leave;
471         }
472
473         num_free_extents = ocfs2_num_free_extents(osb,
474                                                   inode,
475                                                   fe);
476         if (num_free_extents < 0) {
477                 status = num_free_extents;
478                 mlog_errno(status);
479                 goto leave;
480         }
481
482         if (!num_free_extents) {
483                 status = ocfs2_reserve_new_metadata(osb,
484                                                     handle,
485                                                     fe,
486                                                     &meta_ac);
487                 if (status < 0) {
488                         if (status != -ENOSPC)
489                                 mlog_errno(status);
490                         goto leave;
491                 }
492         }
493
494         status = ocfs2_reserve_clusters(osb,
495                                         handle,
496                                         clusters_to_add,
497                                         &data_ac);
498         if (status < 0) {
499                 if (status != -ENOSPC)
500                         mlog_errno(status);
501                 goto leave;
502         }
503
504         /* blocks peope in read/write from reading our allocation
505          * until we're done changing it. We depend on i_mutex to block
506          * other extend/truncate calls while we're here. Ordering wrt
507          * start_trans is important here -- always do it before! */
508         down_write(&OCFS2_I(inode)->ip_alloc_sem);
509         drop_alloc_sem = 1;
510
511         credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
512         handle = ocfs2_start_trans(osb, handle, credits);
513         if (IS_ERR(handle)) {
514                 status = PTR_ERR(handle);
515                 handle = NULL;
516                 mlog_errno(status);
517                 goto leave;
518         }
519
520 restarted_transaction:
521         /* reserve a write to the file entry early on - that we if we
522          * run out of credits in the allocation path, we can still
523          * update i_size. */
524         status = ocfs2_journal_access(handle, inode, bh,
525                                       OCFS2_JOURNAL_ACCESS_WRITE);
526         if (status < 0) {
527                 mlog_errno(status);
528                 goto leave;
529         }
530
531         prev_clusters = OCFS2_I(inode)->ip_clusters;
532
533         status = ocfs2_do_extend_allocation(osb,
534                                             inode,
535                                             clusters_to_add,
536                                             bh,
537                                             handle,
538                                             data_ac,
539                                             meta_ac,
540                                             &why);
541         if ((status < 0) && (status != -EAGAIN)) {
542                 if (status != -ENOSPC)
543                         mlog_errno(status);
544                 goto leave;
545         }
546
547         status = ocfs2_journal_dirty(handle, bh);
548         if (status < 0) {
549                 mlog_errno(status);
550                 goto leave;
551         }
552
553         spin_lock(&OCFS2_I(inode)->ip_lock);
554         clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
555         spin_unlock(&OCFS2_I(inode)->ip_lock);
556
557         if (why != RESTART_NONE && clusters_to_add) {
558                 if (why == RESTART_META) {
559                         mlog(0, "restarting function.\n");
560                         restart_func = 1;
561                 } else {
562                         BUG_ON(why != RESTART_TRANS);
563
564                         mlog(0, "restarting transaction.\n");
565                         /* TODO: This can be more intelligent. */
566                         credits = ocfs2_calc_extend_credits(osb->sb,
567                                                             fe,
568                                                             clusters_to_add);
569                         status = ocfs2_extend_trans(handle, credits);
570                         if (status < 0) {
571                                 /* handle still has to be committed at
572                                  * this point. */
573                                 status = -ENOMEM;
574                                 mlog_errno(status);
575                                 goto leave;
576                         }
577                         goto restarted_transaction;
578                 }
579         }
580
581         mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
582              fe->i_clusters, (unsigned long long)fe->i_size);
583         mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
584              OCFS2_I(inode)->ip_clusters, i_size_read(inode));
585
586 leave:
587         if (drop_alloc_sem) {
588                 up_write(&OCFS2_I(inode)->ip_alloc_sem);
589                 drop_alloc_sem = 0;
590         }
591         if (handle) {
592                 ocfs2_commit_trans(handle);
593                 handle = NULL;
594         }
595         if (data_ac) {
596                 ocfs2_free_alloc_context(data_ac);
597                 data_ac = NULL;
598         }
599         if (meta_ac) {
600                 ocfs2_free_alloc_context(meta_ac);
601                 meta_ac = NULL;
602         }
603         if ((!status) && restart_func) {
604                 restart_func = 0;
605                 goto restart_all;
606         }
607         if (bh) {
608                 brelse(bh);
609                 bh = NULL;
610         }
611
612         mlog_exit(status);
613         return status;
614 }
615
616 /* Some parts of this taken from generic_cont_expand, which turned out
617  * to be too fragile to do exactly what we need without us having to
618  * worry about recursive locking in ->prepare_write() and
619  * ->commit_write(). */
620 static int ocfs2_write_zero_page(struct inode *inode,
621                                  u64 size)
622 {
623         struct address_space *mapping = inode->i_mapping;
624         struct page *page;
625         unsigned long index;
626         unsigned int offset;
627         struct ocfs2_journal_handle *handle = NULL;
628         int ret;
629
630         offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
631         /* ugh.  in prepare/commit_write, if from==to==start of block, we 
632         ** skip the prepare.  make sure we never send an offset for the start
633         ** of a block
634         */
635         if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
636                 offset++;
637         }
638         index = size >> PAGE_CACHE_SHIFT;
639
640         page = grab_cache_page(mapping, index);
641         if (!page) {
642                 ret = -ENOMEM;
643                 mlog_errno(ret);
644                 goto out;
645         }
646
647         ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
648         if (ret < 0) {
649                 mlog_errno(ret);
650                 goto out_unlock;
651         }
652
653         if (ocfs2_should_order_data(inode)) {
654                 handle = ocfs2_start_walk_page_trans(inode, page, offset,
655                                                      offset);
656                 if (IS_ERR(handle)) {
657                         ret = PTR_ERR(handle);
658                         handle = NULL;
659                         goto out_unlock;
660                 }
661         }
662
663         /* must not update i_size! */
664         ret = block_commit_write(page, offset, offset);
665         if (ret < 0)
666                 mlog_errno(ret);
667         else
668                 ret = 0;
669
670         if (handle)
671                 ocfs2_commit_trans(handle);
672 out_unlock:
673         unlock_page(page);
674         page_cache_release(page);
675 out:
676         return ret;
677 }
678
679 static int ocfs2_zero_extend(struct inode *inode,
680                              u64 zero_to_size)
681 {
682         int ret = 0;
683         u64 start_off;
684         struct super_block *sb = inode->i_sb;
685
686         start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
687         while (start_off < zero_to_size) {
688                 ret = ocfs2_write_zero_page(inode, start_off);
689                 if (ret < 0) {
690                         mlog_errno(ret);
691                         goto out;
692                 }
693
694                 start_off += sb->s_blocksize;
695
696                 /*
697                  * Very large extends have the potential to lock up
698                  * the cpu for extended periods of time.
699                  */
700                 cond_resched();
701         }
702
703 out:
704         return ret;
705 }
706
707 /* 
708  * A tail_to_skip value > 0 indicates that we're being called from
709  * ocfs2_file_aio_write(). This has the following implications:
710  *
711  * - we don't want to update i_size
712  * - di_bh will be NULL, which is fine because it's only used in the
713  *   case where we want to update i_size.
714  * - ocfs2_zero_extend() will then only be filling the hole created
715  *   between i_size and the start of the write.
716  */
717 static int ocfs2_extend_file(struct inode *inode,
718                              struct buffer_head *di_bh,
719                              u64 new_i_size,
720                              size_t tail_to_skip)
721 {
722         int ret = 0;
723         u32 clusters_to_add;
724
725         BUG_ON(!tail_to_skip && !di_bh);
726
727         /* setattr sometimes calls us like this. */
728         if (new_i_size == 0)
729                 goto out;
730
731         if (i_size_read(inode) == new_i_size)
732                 goto out;
733         BUG_ON(new_i_size < i_size_read(inode));
734
735         clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) - 
736                 OCFS2_I(inode)->ip_clusters;
737
738         /* 
739          * protect the pages that ocfs2_zero_extend is going to be
740          * pulling into the page cache.. we do this before the
741          * metadata extend so that we don't get into the situation
742          * where we've extended the metadata but can't get the data
743          * lock to zero.
744          */
745         ret = ocfs2_data_lock(inode, 1);
746         if (ret < 0) {
747                 mlog_errno(ret);
748                 goto out;
749         }
750
751         if (clusters_to_add) {
752                 ret = ocfs2_extend_allocation(inode, clusters_to_add);
753                 if (ret < 0) {
754                         mlog_errno(ret);
755                         goto out_unlock;
756                 }
757         }
758
759         /*
760          * Call this even if we don't add any clusters to the tree. We
761          * still need to zero the area between the old i_size and the
762          * new i_size.
763          */
764         ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
765         if (ret < 0) {
766                 mlog_errno(ret);
767                 goto out_unlock;
768         }
769
770         if (!tail_to_skip) {
771                 /* We're being called from ocfs2_setattr() which wants
772                  * us to update i_size */
773                 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
774                 if (ret < 0)
775                         mlog_errno(ret);
776         }
777
778 out_unlock:
779         ocfs2_data_unlock(inode, 1);
780
781 out:
782         return ret;
783 }
784
785 int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
786 {
787         int status = 0, size_change;
788         struct inode *inode = dentry->d_inode;
789         struct super_block *sb = inode->i_sb;
790         struct ocfs2_super *osb = OCFS2_SB(sb);
791         struct buffer_head *bh = NULL;
792         struct ocfs2_journal_handle *handle = NULL;
793
794         mlog_entry("(0x%p, '%.*s')\n", dentry,
795                    dentry->d_name.len, dentry->d_name.name);
796
797         if (attr->ia_valid & ATTR_MODE)
798                 mlog(0, "mode change: %d\n", attr->ia_mode);
799         if (attr->ia_valid & ATTR_UID)
800                 mlog(0, "uid change: %d\n", attr->ia_uid);
801         if (attr->ia_valid & ATTR_GID)
802                 mlog(0, "gid change: %d\n", attr->ia_gid);
803         if (attr->ia_valid & ATTR_SIZE)
804                 mlog(0, "size change...\n");
805         if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
806                 mlog(0, "time change...\n");
807
808 #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
809                            | ATTR_GID | ATTR_UID | ATTR_MODE)
810         if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
811                 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
812                 return 0;
813         }
814
815         status = inode_change_ok(inode, attr);
816         if (status)
817                 return status;
818
819         size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
820         if (size_change) {
821                 status = ocfs2_rw_lock(inode, 1);
822                 if (status < 0) {
823                         mlog_errno(status);
824                         goto bail;
825                 }
826         }
827
828         status = ocfs2_meta_lock(inode, NULL, &bh, 1);
829         if (status < 0) {
830                 if (status != -ENOENT)
831                         mlog_errno(status);
832                 goto bail_unlock_rw;
833         }
834
835         if (size_change && attr->ia_size != i_size_read(inode)) {
836                 if (i_size_read(inode) > attr->ia_size)
837                         status = ocfs2_truncate_file(inode, bh, attr->ia_size);
838                 else
839                         status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
840                 if (status < 0) {
841                         if (status != -ENOSPC)
842                                 mlog_errno(status);
843                         status = -ENOSPC;
844                         goto bail_unlock;
845                 }
846         }
847
848         handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
849         if (IS_ERR(handle)) {
850                 status = PTR_ERR(handle);
851                 mlog_errno(status);
852                 goto bail_unlock;
853         }
854
855         status = inode_setattr(inode, attr);
856         if (status < 0) {
857                 mlog_errno(status);
858                 goto bail_commit;
859         }
860
861         status = ocfs2_mark_inode_dirty(handle, inode, bh);
862         if (status < 0)
863                 mlog_errno(status);
864
865 bail_commit:
866         ocfs2_commit_trans(handle);
867 bail_unlock:
868         ocfs2_meta_unlock(inode, 1);
869 bail_unlock_rw:
870         if (size_change)
871                 ocfs2_rw_unlock(inode, 1);
872 bail:
873         if (bh)
874                 brelse(bh);
875
876         mlog_exit(status);
877         return status;
878 }
879
880 int ocfs2_getattr(struct vfsmount *mnt,
881                   struct dentry *dentry,
882                   struct kstat *stat)
883 {
884         struct inode *inode = dentry->d_inode;
885         struct super_block *sb = dentry->d_inode->i_sb;
886         struct ocfs2_super *osb = sb->s_fs_info;
887         int err;
888
889         mlog_entry_void();
890
891         err = ocfs2_inode_revalidate(dentry);
892         if (err) {
893                 if (err != -ENOENT)
894                         mlog_errno(err);
895                 goto bail;
896         }
897
898         generic_fillattr(inode, stat);
899
900         /* We set the blksize from the cluster size for performance */
901         stat->blksize = osb->s_clustersize;
902
903 bail:
904         mlog_exit(err);
905
906         return err;
907 }
908
909 static int ocfs2_write_remove_suid(struct inode *inode)
910 {
911         int ret;
912         struct buffer_head *bh = NULL;
913         struct ocfs2_inode_info *oi = OCFS2_I(inode);
914         struct ocfs2_journal_handle *handle;
915         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
916         struct ocfs2_dinode *di;
917
918         mlog_entry("(Inode %llu, mode 0%o)\n",
919                    (unsigned long long)oi->ip_blkno, inode->i_mode);
920
921         handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
922         if (handle == NULL) {
923                 ret = -ENOMEM;
924                 mlog_errno(ret);
925                 goto out;
926         }
927
928         ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
929         if (ret < 0) {
930                 mlog_errno(ret);
931                 goto out_trans;
932         }
933
934         ret = ocfs2_journal_access(handle, inode, bh,
935                                    OCFS2_JOURNAL_ACCESS_WRITE);
936         if (ret < 0) {
937                 mlog_errno(ret);
938                 goto out_bh;
939         }
940
941         inode->i_mode &= ~S_ISUID;
942         if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
943                 inode->i_mode &= ~S_ISGID;
944
945         di = (struct ocfs2_dinode *) bh->b_data;
946         di->i_mode = cpu_to_le16(inode->i_mode);
947
948         ret = ocfs2_journal_dirty(handle, bh);
949         if (ret < 0)
950                 mlog_errno(ret);
951 out_bh:
952         brelse(bh);
953 out_trans:
954         ocfs2_commit_trans(handle);
955 out:
956         mlog_exit(ret);
957         return ret;
958 }
959
960 static inline int ocfs2_write_should_remove_suid(struct inode *inode)
961 {
962         mode_t mode = inode->i_mode;
963
964         if (!capable(CAP_FSETID)) {
965                 if (unlikely(mode & S_ISUID))
966                         return 1;
967
968                 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
969                         return 1;
970         }
971         return 0;
972 }
973
974 static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
975                                     const struct iovec *iov,
976                                     unsigned long nr_segs,
977                                     loff_t pos)
978 {
979         int ret, rw_level = -1, meta_level = -1, have_alloc_sem = 0;
980         u32 clusters;
981         struct file *filp = iocb->ki_filp;
982         struct inode *inode = filp->f_dentry->d_inode;
983         loff_t newsize, saved_pos;
984
985         mlog_entry("(0x%p, %u, '%.*s')\n", filp,
986                    (unsigned int)nr_segs,
987                    filp->f_dentry->d_name.len,
988                    filp->f_dentry->d_name.name);
989
990         /* happy write of zero bytes */
991         if (iocb->ki_left == 0)
992                 return 0;
993
994         if (!inode) {
995                 mlog(0, "bad inode\n");
996                 return -EIO;
997         }
998
999         mutex_lock(&inode->i_mutex);
1000         /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
1001         if (filp->f_flags & O_DIRECT) {
1002                 have_alloc_sem = 1;
1003                 down_read(&inode->i_alloc_sem);
1004         }
1005
1006         /* concurrent O_DIRECT writes are allowed */
1007         rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1;
1008         ret = ocfs2_rw_lock(inode, rw_level);
1009         if (ret < 0) {
1010                 rw_level = -1;
1011                 mlog_errno(ret);
1012                 goto out;
1013         }
1014
1015         /* 
1016          * We sample i_size under a read level meta lock to see if our write
1017          * is extending the file, if it is we back off and get a write level
1018          * meta lock.
1019          */
1020         meta_level = (filp->f_flags & O_APPEND) ? 1 : 0;
1021         for(;;) {
1022                 ret = ocfs2_meta_lock(inode, NULL, NULL, meta_level);
1023                 if (ret < 0) {
1024                         meta_level = -1;
1025                         mlog_errno(ret);
1026                         goto out;
1027                 }
1028
1029                 /* Clear suid / sgid if necessary. We do this here
1030                  * instead of later in the write path because
1031                  * remove_suid() calls ->setattr without any hint that
1032                  * we may have already done our cluster locking. Since
1033                  * ocfs2_setattr() *must* take cluster locks to
1034                  * proceeed, this will lead us to recursively lock the
1035                  * inode. There's also the dinode i_size state which
1036                  * can be lost via setattr during extending writes (we
1037                  * set inode->i_size at the end of a write. */
1038                 if (ocfs2_write_should_remove_suid(inode)) {
1039                         if (meta_level == 0) {
1040                                 ocfs2_meta_unlock(inode, meta_level);
1041                                 meta_level = 1;
1042                                 continue;
1043                         }
1044
1045                         ret = ocfs2_write_remove_suid(inode);
1046                         if (ret < 0) {
1047                                 mlog_errno(ret);
1048                                 goto out;
1049                         }
1050                 }
1051
1052                 /* work on a copy of ppos until we're sure that we won't have
1053                  * to recalculate it due to relocking. */
1054                 if (filp->f_flags & O_APPEND) {
1055                         saved_pos = i_size_read(inode);
1056                         mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1057                 } else {
1058                         saved_pos = iocb->ki_pos;
1059                 }
1060                 newsize = iocb->ki_left + saved_pos;
1061
1062                 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1063                      (long long) saved_pos, (long long) newsize,
1064                      (long long) i_size_read(inode));
1065
1066                 /* No need for a higher level metadata lock if we're
1067                  * never going past i_size. */
1068                 if (newsize <= i_size_read(inode))
1069                         break;
1070
1071                 if (meta_level == 0) {
1072                         ocfs2_meta_unlock(inode, meta_level);
1073                         meta_level = 1;
1074                         continue;
1075                 }
1076
1077                 spin_lock(&OCFS2_I(inode)->ip_lock);
1078                 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1079                         OCFS2_I(inode)->ip_clusters;
1080                 spin_unlock(&OCFS2_I(inode)->ip_lock);
1081
1082                 mlog(0, "Writing at EOF, may need more allocation: "
1083                      "i_size = %lld, newsize = %lld, need %u clusters\n",
1084                      (long long) i_size_read(inode), (long long) newsize,
1085                      clusters);
1086
1087                 /* We only want to continue the rest of this loop if
1088                  * our extend will actually require more
1089                  * allocation. */
1090                 if (!clusters)
1091                         break;
1092
1093                 ret = ocfs2_extend_file(inode, NULL, newsize, iocb->ki_left);
1094                 if (ret < 0) {
1095                         if (ret != -ENOSPC)
1096                                 mlog_errno(ret);
1097                         goto out;
1098                 }
1099                 break;
1100         }
1101
1102         /* ok, we're done with i_size and alloc work */
1103         iocb->ki_pos = saved_pos;
1104         ocfs2_meta_unlock(inode, meta_level);
1105         meta_level = -1;
1106
1107         /* communicate with ocfs2_dio_end_io */
1108         ocfs2_iocb_set_rw_locked(iocb);
1109
1110         ret = generic_file_aio_write_nolock(iocb, iov, nr_segs, iocb->ki_pos);
1111
1112         /* buffered aio wouldn't have proper lock coverage today */
1113         BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1114
1115         /* 
1116          * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1117          * function pointer which is called when o_direct io completes so that
1118          * it can unlock our rw lock.  (it's the clustered equivalent of
1119          * i_alloc_sem; protects truncate from racing with pending ios).
1120          * Unfortunately there are error cases which call end_io and others
1121          * that don't.  so we don't have to unlock the rw_lock if either an
1122          * async dio is going to do it in the future or an end_io after an
1123          * error has already done it.
1124          */
1125         if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1126                 rw_level = -1;
1127                 have_alloc_sem = 0;
1128         }
1129
1130 out:
1131         if (meta_level != -1)
1132                 ocfs2_meta_unlock(inode, meta_level);
1133         if (have_alloc_sem)
1134                 up_read(&inode->i_alloc_sem);
1135         if (rw_level != -1) 
1136                 ocfs2_rw_unlock(inode, rw_level);
1137         mutex_unlock(&inode->i_mutex);
1138
1139         mlog_exit(ret);
1140         return ret;
1141 }
1142
1143 static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
1144                                    const struct iovec *iov,
1145                                    unsigned long nr_segs,
1146                                    loff_t pos)
1147 {
1148         int ret = 0, rw_level = -1, have_alloc_sem = 0;
1149         struct file *filp = iocb->ki_filp;
1150         struct inode *inode = filp->f_dentry->d_inode;
1151
1152         mlog_entry("(0x%p, %u, '%.*s')\n", filp,
1153                    (unsigned int)nr_segs,
1154                    filp->f_dentry->d_name.len,
1155                    filp->f_dentry->d_name.name);
1156
1157         if (!inode) {
1158                 ret = -EINVAL;
1159                 mlog_errno(ret);
1160                 goto bail;
1161         }
1162
1163         /* 
1164          * buffered reads protect themselves in ->readpage().  O_DIRECT reads
1165          * need locks to protect pending reads from racing with truncate.
1166          */
1167         if (filp->f_flags & O_DIRECT) {
1168                 down_read(&inode->i_alloc_sem);
1169                 have_alloc_sem = 1;
1170
1171                 ret = ocfs2_rw_lock(inode, 0);
1172                 if (ret < 0) {
1173                         mlog_errno(ret);
1174                         goto bail;
1175                 }
1176                 rw_level = 0;
1177                 /* communicate with ocfs2_dio_end_io */
1178                 ocfs2_iocb_set_rw_locked(iocb);
1179         }
1180
1181         /*
1182          * We're fine letting folks race truncates and extending
1183          * writes with read across the cluster, just like they can
1184          * locally. Hence no rw_lock during read.
1185          * 
1186          * Take and drop the meta data lock to update inode fields
1187          * like i_size. This allows the checks down below
1188          * generic_file_aio_read() a chance of actually working. 
1189          */
1190         ret = ocfs2_meta_lock(inode, NULL, NULL, 0);
1191         if (ret < 0) {
1192                 mlog_errno(ret);
1193                 goto bail;
1194         }
1195         ocfs2_meta_unlock(inode, 0);
1196
1197         ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
1198         if (ret == -EINVAL)
1199                 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
1200
1201         /* buffered aio wouldn't have proper lock coverage today */
1202         BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
1203
1204         /* see ocfs2_file_aio_write */
1205         if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
1206                 rw_level = -1;
1207                 have_alloc_sem = 0;
1208         }
1209
1210 bail:
1211         if (have_alloc_sem)
1212                 up_read(&inode->i_alloc_sem);
1213         if (rw_level != -1) 
1214                 ocfs2_rw_unlock(inode, rw_level);
1215         mlog_exit(ret);
1216
1217         return ret;
1218 }
1219
1220 struct inode_operations ocfs2_file_iops = {
1221         .setattr        = ocfs2_setattr,
1222         .getattr        = ocfs2_getattr,
1223 };
1224
1225 struct inode_operations ocfs2_special_file_iops = {
1226         .setattr        = ocfs2_setattr,
1227         .getattr        = ocfs2_getattr,
1228 };
1229
1230 const struct file_operations ocfs2_fops = {
1231         .read           = do_sync_read,
1232         .write          = do_sync_write,
1233         .sendfile       = generic_file_sendfile,
1234         .mmap           = ocfs2_mmap,
1235         .fsync          = ocfs2_sync_file,
1236         .release        = ocfs2_file_release,
1237         .open           = ocfs2_file_open,
1238         .aio_read       = ocfs2_file_aio_read,
1239         .aio_write      = ocfs2_file_aio_write,
1240         .ioctl          = ocfs2_ioctl,
1241 };
1242
1243 const struct file_operations ocfs2_dops = {
1244         .read           = generic_read_dir,
1245         .readdir        = ocfs2_readdir,
1246         .fsync          = ocfs2_sync_file,
1247         .ioctl          = ocfs2_ioctl,
1248 };