ocfs2: teach extend/truncate about sparse files
[powerpc.git] / fs / ocfs2 / inode.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * inode.c
5  *
6  * vfs' aops, fops, dops and iops
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/fs.h>
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/pagemap.h>
31 #include <linux/smp_lock.h>
32
33 #include <asm/byteorder.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 "dlmglue.h"
42 #include "extent_map.h"
43 #include "file.h"
44 #include "heartbeat.h"
45 #include "inode.h"
46 #include "journal.h"
47 #include "namei.h"
48 #include "suballoc.h"
49 #include "super.h"
50 #include "symlink.h"
51 #include "sysfile.h"
52 #include "uptodate.h"
53 #include "vote.h"
54
55 #include "buffer_head_io.h"
56
57 struct ocfs2_find_inode_args
58 {
59         u64             fi_blkno;
60         unsigned long   fi_ino;
61         unsigned int    fi_flags;
62 };
63
64 static int ocfs2_read_locked_inode(struct inode *inode,
65                                    struct ocfs2_find_inode_args *args);
66 static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
67 static int ocfs2_find_actor(struct inode *inode, void *opaque);
68 static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
69                                     struct inode *inode,
70                                     struct buffer_head *fe_bh);
71
72 void ocfs2_set_inode_flags(struct inode *inode)
73 {
74         unsigned int flags = OCFS2_I(inode)->ip_attr;
75
76         inode->i_flags &= ~(S_IMMUTABLE |
77                 S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
78
79         if (flags & OCFS2_IMMUTABLE_FL)
80                 inode->i_flags |= S_IMMUTABLE;
81
82         if (flags & OCFS2_SYNC_FL)
83                 inode->i_flags |= S_SYNC;
84         if (flags & OCFS2_APPEND_FL)
85                 inode->i_flags |= S_APPEND;
86         if (flags & OCFS2_NOATIME_FL)
87                 inode->i_flags |= S_NOATIME;
88         if (flags & OCFS2_DIRSYNC_FL)
89                 inode->i_flags |= S_DIRSYNC;
90 }
91
92 struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, int flags)
93 {
94         struct inode *inode = NULL;
95         struct super_block *sb = osb->sb;
96         struct ocfs2_find_inode_args args;
97
98         mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno);
99
100         /* Ok. By now we've either got the offsets passed to us by the
101          * caller, or we just pulled them off the bh. Lets do some
102          * sanity checks to make sure they're OK. */
103         if (blkno == 0) {
104                 inode = ERR_PTR(-EINVAL);
105                 mlog_errno(PTR_ERR(inode));
106                 goto bail;
107         }
108
109         args.fi_blkno = blkno;
110         args.fi_flags = flags;
111         args.fi_ino = ino_from_blkno(sb, blkno);
112
113         inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
114                              ocfs2_init_locked_inode, &args);
115         /* inode was *not* in the inode cache. 2.6.x requires
116          * us to do our own read_inode call and unlock it
117          * afterwards. */
118         if (inode && inode->i_state & I_NEW) {
119                 mlog(0, "Inode was not in inode cache, reading it.\n");
120                 ocfs2_read_locked_inode(inode, &args);
121                 unlock_new_inode(inode);
122         }
123         if (inode == NULL) {
124                 inode = ERR_PTR(-ENOMEM);
125                 mlog_errno(PTR_ERR(inode));
126                 goto bail;
127         }
128         if (is_bad_inode(inode)) {
129                 iput(inode);
130                 inode = ERR_PTR(-ESTALE);
131                 goto bail;
132         }
133
134 bail:
135         if (!IS_ERR(inode)) {
136                 mlog(0, "returning inode with number %llu\n",
137                      (unsigned long long)OCFS2_I(inode)->ip_blkno);
138                 mlog_exit_ptr(inode);
139         }
140
141         return inode;
142 }
143
144
145 /*
146  * here's how inodes get read from disk:
147  * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
148  * found? : return the in-memory inode
149  * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
150  */
151
152 static int ocfs2_find_actor(struct inode *inode, void *opaque)
153 {
154         struct ocfs2_find_inode_args *args = NULL;
155         struct ocfs2_inode_info *oi = OCFS2_I(inode);
156         int ret = 0;
157
158         mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);
159
160         args = opaque;
161
162         mlog_bug_on_msg(!inode, "No inode in find actor!\n");
163
164         if (oi->ip_blkno != args->fi_blkno)
165                 goto bail;
166
167         ret = 1;
168 bail:
169         mlog_exit(ret);
170         return ret;
171 }
172
173 /*
174  * initialize the new inode, but don't do anything that would cause
175  * us to sleep.
176  * return 0 on success, 1 on failure
177  */
178 static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
179 {
180         struct ocfs2_find_inode_args *args = opaque;
181
182         mlog_entry("inode = %p, opaque = %p\n", inode, opaque);
183
184         inode->i_ino = args->fi_ino;
185         OCFS2_I(inode)->ip_blkno = args->fi_blkno;
186
187         mlog_exit(0);
188         return 0;
189 }
190
191 int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
192                          int create_ino)
193 {
194         struct super_block *sb;
195         struct ocfs2_super *osb;
196         int status = -EINVAL;
197
198         mlog_entry("(0x%p, size:%llu)\n", inode,
199                    (unsigned long long)fe->i_size);
200
201         sb = inode->i_sb;
202         osb = OCFS2_SB(sb);
203
204         /* this means that read_inode cannot create a superblock inode
205          * today.  change if needed. */
206         if (!OCFS2_IS_VALID_DINODE(fe) ||
207             !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
208                 mlog(0, "Invalid dinode: i_ino=%lu, i_blkno=%llu, "
209                      "signature = %.*s, flags = 0x%x\n",
210                      inode->i_ino,
211                      (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
212                      fe->i_signature, le32_to_cpu(fe->i_flags));
213                 goto bail;
214         }
215
216         if (le32_to_cpu(fe->i_fs_generation) != osb->fs_generation) {
217                 mlog(ML_ERROR, "file entry generation does not match "
218                      "superblock! osb->fs_generation=%x, "
219                      "fe->i_fs_generation=%x\n",
220                      osb->fs_generation, le32_to_cpu(fe->i_fs_generation));
221                 goto bail;
222         }
223
224         inode->i_version = 1;
225         inode->i_generation = le32_to_cpu(fe->i_generation);
226         inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
227         inode->i_mode = le16_to_cpu(fe->i_mode);
228         inode->i_uid = le32_to_cpu(fe->i_uid);
229         inode->i_gid = le32_to_cpu(fe->i_gid);
230
231         /* Fast symlinks will have i_size but no allocated clusters. */
232         if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
233                 inode->i_blocks = 0;
234         else
235                 inode->i_blocks =
236                         ocfs2_align_bytes_to_sectors(le64_to_cpu(fe->i_size));
237         inode->i_mapping->a_ops = &ocfs2_aops;
238         inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
239         inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
240         inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
241         inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
242         inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
243         inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
244
245         if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
246                 mlog(ML_ERROR,
247                      "ip_blkno %llu != i_blkno %llu!\n",
248                      (unsigned long long)OCFS2_I(inode)->ip_blkno,
249                      (unsigned long long)fe->i_blkno);
250
251         OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
252         OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
253
254         inode->i_nlink = le16_to_cpu(fe->i_links_count);
255
256         if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL))
257                 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
258
259         if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
260                 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
261                 mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino);
262         } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
263                 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
264         } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
265                 mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino);
266                 /* we can't actually hit this as read_inode can't
267                  * handle superblocks today ;-) */
268                 BUG();
269         }
270
271         switch (inode->i_mode & S_IFMT) {
272             case S_IFREG:
273                     inode->i_fop = &ocfs2_fops;
274                     inode->i_op = &ocfs2_file_iops;
275                     i_size_write(inode, le64_to_cpu(fe->i_size));
276                     break;
277             case S_IFDIR:
278                     inode->i_op = &ocfs2_dir_iops;
279                     inode->i_fop = &ocfs2_dops;
280                     i_size_write(inode, le64_to_cpu(fe->i_size));
281                     break;
282             case S_IFLNK:
283                     if (ocfs2_inode_is_fast_symlink(inode))
284                         inode->i_op = &ocfs2_fast_symlink_inode_operations;
285                     else
286                         inode->i_op = &ocfs2_symlink_inode_operations;
287                     i_size_write(inode, le64_to_cpu(fe->i_size));
288                     break;
289             default:
290                     inode->i_op = &ocfs2_special_file_iops;
291                     init_special_inode(inode, inode->i_mode,
292                                        inode->i_rdev);
293                     break;
294         }
295
296         if (create_ino) {
297                 inode->i_ino = ino_from_blkno(inode->i_sb,
298                                le64_to_cpu(fe->i_blkno));
299
300                 /*
301                  * If we ever want to create system files from kernel,
302                  * the generation argument to
303                  * ocfs2_inode_lock_res_init() will have to change.
304                  */
305                 BUG_ON(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL));
306
307                 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_meta_lockres,
308                                           OCFS2_LOCK_TYPE_META, 0, inode);
309
310                 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
311                                           OCFS2_LOCK_TYPE_OPEN, 0, inode);
312         }
313
314         ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
315                                   OCFS2_LOCK_TYPE_RW, inode->i_generation,
316                                   inode);
317
318         ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_data_lockres,
319                                   OCFS2_LOCK_TYPE_DATA, inode->i_generation,
320                                   inode);
321
322         ocfs2_set_inode_flags(inode);
323
324         status = 0;
325 bail:
326         mlog_exit(status);
327         return status;
328 }
329
330 static int ocfs2_read_locked_inode(struct inode *inode,
331                                    struct ocfs2_find_inode_args *args)
332 {
333         struct super_block *sb;
334         struct ocfs2_super *osb;
335         struct ocfs2_dinode *fe;
336         struct buffer_head *bh = NULL;
337         int status, can_lock;
338         u32 generation = 0;
339
340         mlog_entry("(0x%p, 0x%p)\n", inode, args);
341
342         status = -EINVAL;
343         if (inode == NULL || inode->i_sb == NULL) {
344                 mlog(ML_ERROR, "bad inode\n");
345                 return status;
346         }
347         sb = inode->i_sb;
348         osb = OCFS2_SB(sb);
349
350         if (!args) {
351                 mlog(ML_ERROR, "bad inode args\n");
352                 make_bad_inode(inode);
353                 return status;
354         }
355
356         /*
357          * To improve performance of cold-cache inode stats, we take
358          * the cluster lock here if possible.
359          *
360          * Generally, OCFS2 never trusts the contents of an inode
361          * unless it's holding a cluster lock, so taking it here isn't
362          * a correctness issue as much as it is a performance
363          * improvement.
364          *
365          * There are three times when taking the lock is not a good idea:
366          *
367          * 1) During startup, before we have initialized the DLM.
368          *
369          * 2) If we are reading certain system files which never get
370          *    cluster locks (local alloc, truncate log).
371          *
372          * 3) If the process doing the iget() is responsible for
373          *    orphan dir recovery. We're holding the orphan dir lock and
374          *    can get into a deadlock with another process on another
375          *    node in ->delete_inode().
376          *
377          * #1 and #2 can be simply solved by never taking the lock
378          * here for system files (which are the only type we read
379          * during mount). It's a heavier approach, but our main
380          * concern is user-accesible files anyway.
381          *
382          * #3 works itself out because we'll eventually take the
383          * cluster lock before trusting anything anyway.
384          */
385         can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
386                 && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
387                 && !ocfs2_mount_local(osb);
388
389         /*
390          * To maintain backwards compatibility with older versions of
391          * ocfs2-tools, we still store the generation value for system
392          * files. The only ones that actually matter to userspace are
393          * the journals, but it's easier and inexpensive to just flag
394          * all system files similarly.
395          */
396         if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
397                 generation = osb->fs_generation;
398
399         ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_meta_lockres,
400                                   OCFS2_LOCK_TYPE_META,
401                                   generation, inode);
402
403         ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
404                                   OCFS2_LOCK_TYPE_OPEN,
405                                   0, inode);
406
407         if (can_lock) {
408                 status = ocfs2_open_lock(inode);
409                 if (status) {
410                         make_bad_inode(inode);
411                         mlog_errno(status);
412                         return status;
413                 }
414                 status = ocfs2_meta_lock(inode, NULL, 0);
415                 if (status) {
416                         make_bad_inode(inode);
417                         mlog_errno(status);
418                         return status;
419                 }
420         }
421
422         if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
423                 status = ocfs2_try_open_lock(inode, 0);
424                 if (status) {
425                         make_bad_inode(inode);  
426                         return status;
427                 }
428         }
429
430         status = ocfs2_read_block(osb, args->fi_blkno, &bh, 0,
431                                   can_lock ? inode : NULL);
432         if (status < 0) {
433                 mlog_errno(status);
434                 goto bail;
435         }
436
437         status = -EINVAL;
438         fe = (struct ocfs2_dinode *) bh->b_data;
439         if (!OCFS2_IS_VALID_DINODE(fe)) {
440                 mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n",
441                      (unsigned long long)fe->i_blkno, 7, fe->i_signature);
442                 goto bail;
443         }
444
445         /*
446          * This is a code bug. Right now the caller needs to
447          * understand whether it is asking for a system file inode or
448          * not so the proper lock names can be built.
449          */
450         mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
451                         !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
452                         "Inode %llu: system file state is ambigous\n",
453                         (unsigned long long)args->fi_blkno);
454
455         if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
456             S_ISBLK(le16_to_cpu(fe->i_mode)))
457                 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
458
459         if (ocfs2_populate_inode(inode, fe, 0) < 0)
460                 goto bail;
461
462         BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
463
464         status = 0;
465
466 bail:
467         if (can_lock)
468                 ocfs2_meta_unlock(inode, 0);
469
470         if (status < 0)
471                 make_bad_inode(inode);
472
473         if (args && bh)
474                 brelse(bh);
475
476         mlog_exit(status);
477         return status;
478 }
479
480 void ocfs2_sync_blockdev(struct super_block *sb)
481 {
482         sync_blockdev(sb->s_bdev);
483 }
484
485 static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
486                                      struct inode *inode,
487                                      struct buffer_head *fe_bh)
488 {
489         int status = 0;
490         struct ocfs2_truncate_context *tc = NULL;
491         struct ocfs2_dinode *fe;
492
493         mlog_entry_void();
494
495         fe = (struct ocfs2_dinode *) fe_bh->b_data;
496
497         if (fe->i_clusters) {
498                 status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc);
499                 if (status < 0) {
500                         mlog_errno(status);
501                         goto out;
502                 }
503
504                 status = ocfs2_commit_truncate(osb, inode, fe_bh, tc);
505                 if (status < 0) {
506                         mlog_errno(status);
507                         goto out;
508                 }
509         }
510 out:
511
512         mlog_exit(status);
513         return status;
514 }
515
516 static int ocfs2_remove_inode(struct inode *inode,
517                               struct buffer_head *di_bh,
518                               struct inode *orphan_dir_inode,
519                               struct buffer_head *orphan_dir_bh)
520 {
521         int status;
522         struct inode *inode_alloc_inode = NULL;
523         struct buffer_head *inode_alloc_bh = NULL;
524         handle_t *handle;
525         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
526         struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
527
528         inode_alloc_inode =
529                 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
530                                             le16_to_cpu(di->i_suballoc_slot));
531         if (!inode_alloc_inode) {
532                 status = -EEXIST;
533                 mlog_errno(status);
534                 goto bail;
535         }
536
537         mutex_lock(&inode_alloc_inode->i_mutex);
538         status = ocfs2_meta_lock(inode_alloc_inode, &inode_alloc_bh, 1);
539         if (status < 0) {
540                 mutex_unlock(&inode_alloc_inode->i_mutex);
541
542                 mlog_errno(status);
543                 goto bail;
544         }
545
546         handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS);
547         if (IS_ERR(handle)) {
548                 status = PTR_ERR(handle);
549                 mlog_errno(status);
550                 goto bail_unlock;
551         }
552
553         status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
554                                   orphan_dir_bh);
555         if (status < 0) {
556                 mlog_errno(status);
557                 goto bail_commit;
558         }
559
560         /* set the inodes dtime */
561         status = ocfs2_journal_access(handle, inode, di_bh,
562                                       OCFS2_JOURNAL_ACCESS_WRITE);
563         if (status < 0) {
564                 mlog_errno(status);
565                 goto bail_commit;
566         }
567
568         di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
569         le32_and_cpu(&di->i_flags, ~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
570
571         status = ocfs2_journal_dirty(handle, di_bh);
572         if (status < 0) {
573                 mlog_errno(status);
574                 goto bail_commit;
575         }
576
577         ocfs2_remove_from_cache(inode, di_bh);
578
579         status = ocfs2_free_dinode(handle, inode_alloc_inode,
580                                    inode_alloc_bh, di);
581         if (status < 0)
582                 mlog_errno(status);
583
584 bail_commit:
585         ocfs2_commit_trans(osb, handle);
586 bail_unlock:
587         ocfs2_meta_unlock(inode_alloc_inode, 1);
588         mutex_unlock(&inode_alloc_inode->i_mutex);
589         brelse(inode_alloc_bh);
590 bail:
591         iput(inode_alloc_inode);
592
593         return status;
594 }
595
596 /* 
597  * Serialize with orphan dir recovery. If the process doing
598  * recovery on this orphan dir does an iget() with the dir
599  * i_mutex held, we'll deadlock here. Instead we detect this
600  * and exit early - recovery will wipe this inode for us.
601  */
602 static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
603                                              int slot)
604 {
605         int ret = 0;
606
607         spin_lock(&osb->osb_lock);
608         if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
609                 mlog(0, "Recovery is happening on orphan dir %d, will skip "
610                      "this inode\n", slot);
611                 ret = -EDEADLK;
612                 goto out;
613         }
614         /* This signals to the orphan recovery process that it should
615          * wait for us to handle the wipe. */
616         osb->osb_orphan_wipes[slot]++;
617 out:
618         spin_unlock(&osb->osb_lock);
619         return ret;
620 }
621
622 static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
623                                          int slot)
624 {
625         spin_lock(&osb->osb_lock);
626         osb->osb_orphan_wipes[slot]--;
627         spin_unlock(&osb->osb_lock);
628
629         wake_up(&osb->osb_wipe_event);
630 }
631
632 static int ocfs2_wipe_inode(struct inode *inode,
633                             struct buffer_head *di_bh)
634 {
635         int status, orphaned_slot;
636         struct inode *orphan_dir_inode = NULL;
637         struct buffer_head *orphan_dir_bh = NULL;
638         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
639         struct ocfs2_dinode *di;
640
641         di = (struct ocfs2_dinode *) di_bh->b_data;
642         orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
643
644         status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
645         if (status)
646                 return status;
647
648         orphan_dir_inode = ocfs2_get_system_file_inode(osb,
649                                                        ORPHAN_DIR_SYSTEM_INODE,
650                                                        orphaned_slot);
651         if (!orphan_dir_inode) {
652                 status = -EEXIST;
653                 mlog_errno(status);
654                 goto bail;
655         }
656
657         /* Lock the orphan dir. The lock will be held for the entire
658          * delete_inode operation. We do this now to avoid races with
659          * recovery completion on other nodes. */
660         mutex_lock(&orphan_dir_inode->i_mutex);
661         status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1);
662         if (status < 0) {
663                 mutex_unlock(&orphan_dir_inode->i_mutex);
664
665                 mlog_errno(status);
666                 goto bail;
667         }
668
669         /* we do this while holding the orphan dir lock because we
670          * don't want recovery being run from another node to vote for
671          * an inode delete on us -- this will result in two nodes
672          * truncating the same file! */
673         status = ocfs2_truncate_for_delete(osb, inode, di_bh);
674         if (status < 0) {
675                 mlog_errno(status);
676                 goto bail_unlock_dir;
677         }
678
679         status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
680                                     orphan_dir_bh);
681         if (status < 0)
682                 mlog_errno(status);
683
684 bail_unlock_dir:
685         ocfs2_meta_unlock(orphan_dir_inode, 1);
686         mutex_unlock(&orphan_dir_inode->i_mutex);
687         brelse(orphan_dir_bh);
688 bail:
689         iput(orphan_dir_inode);
690         ocfs2_signal_wipe_completion(osb, orphaned_slot);
691
692         return status;
693 }
694
695 /* There is a series of simple checks that should be done before a
696  * vote is even considered. Encapsulate those in this function. */
697 static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
698 {
699         int ret = 0;
700         struct ocfs2_inode_info *oi = OCFS2_I(inode);
701         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
702
703         /* We shouldn't be getting here for the root directory
704          * inode.. */
705         if (inode == osb->root_inode) {
706                 mlog(ML_ERROR, "Skipping delete of root inode.\n");
707                 goto bail;
708         }
709
710         /* If we're coming from process_vote we can't go into our own
711          * voting [hello, deadlock city!], so unforuntately we just
712          * have to skip deleting this guy. That's OK though because
713          * the node who's doing the actual deleting should handle it
714          * anyway. */
715         if (current == osb->vote_task) {
716                 mlog(0, "Skipping delete of %lu because we're currently "
717                      "in process_vote\n", inode->i_ino);
718                 goto bail;
719         }
720
721         spin_lock(&oi->ip_lock);
722         /* OCFS2 *never* deletes system files. This should technically
723          * never get here as system file inodes should always have a
724          * positive link count. */
725         if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
726                 mlog(ML_ERROR, "Skipping delete of system file %llu\n",
727                      (unsigned long long)oi->ip_blkno);
728                 goto bail_unlock;
729         }
730
731         /* If we have voted "yes" on the wipe of this inode for
732          * another node, it will be marked here so we can safely skip
733          * it. Recovery will cleanup any inodes we might inadvertantly
734          * skip here. */
735         if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
736                 mlog(0, "Skipping delete of %lu because another node "
737                      "has done this for us.\n", inode->i_ino);
738                 goto bail_unlock;
739         }
740
741         ret = 1;
742 bail_unlock:
743         spin_unlock(&oi->ip_lock);
744 bail:
745         return ret;
746 }
747
748 /* Query the cluster to determine whether we should wipe an inode from
749  * disk or not.
750  *
751  * Requires the inode to have the cluster lock. */
752 static int ocfs2_query_inode_wipe(struct inode *inode,
753                                   struct buffer_head *di_bh,
754                                   int *wipe)
755 {
756         int status = 0;
757         struct ocfs2_inode_info *oi = OCFS2_I(inode);
758         struct ocfs2_dinode *di;
759
760         *wipe = 0;
761
762         /* While we were waiting for the cluster lock in
763          * ocfs2_delete_inode, another node might have asked to delete
764          * the inode. Recheck our flags to catch this. */
765         if (!ocfs2_inode_is_valid_to_delete(inode)) {
766                 mlog(0, "Skipping delete of %llu because flags changed\n",
767                      (unsigned long long)oi->ip_blkno);
768                 goto bail;
769         }
770
771         /* Now that we have an up to date inode, we can double check
772          * the link count. */
773         if (inode->i_nlink) {
774                 mlog(0, "Skipping delete of %llu because nlink = %u\n",
775                      (unsigned long long)oi->ip_blkno, inode->i_nlink);
776                 goto bail;
777         }
778
779         /* Do some basic inode verification... */
780         di = (struct ocfs2_dinode *) di_bh->b_data;
781         if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL))) {
782                 /* for lack of a better error? */
783                 status = -EEXIST;
784                 mlog(ML_ERROR,
785                      "Inode %llu (on-disk %llu) not orphaned! "
786                      "Disk flags  0x%x, inode flags 0x%x\n",
787                      (unsigned long long)oi->ip_blkno,
788                      (unsigned long long)di->i_blkno, di->i_flags,
789                      oi->ip_flags);
790                 goto bail;
791         }
792
793         /* has someone already deleted us?! baaad... */
794         if (di->i_dtime) {
795                 status = -EEXIST;
796                 mlog_errno(status);
797                 goto bail;
798         }
799
800         /*
801          * This is how ocfs2 determines whether an inode is still live
802          * within the cluster. Every node takes a shared read lock on
803          * the inode open lock in ocfs2_read_locked_inode(). When we
804          * get to ->delete_inode(), each node tries to convert it's
805          * lock to an exclusive. Trylocks are serialized by the inode
806          * meta data lock. If the upconvert suceeds, we know the inode
807          * is no longer live and can be deleted.
808          *
809          * Though we call this with the meta data lock held, the
810          * trylock keeps us from ABBA deadlock.
811          */
812         status = ocfs2_try_open_lock(inode, 1);
813         if (status == -EAGAIN) {
814                 status = 0;
815                 mlog(0, "Skipping delete of %llu because it is in use on"
816                      "other nodes\n", (unsigned long long)oi->ip_blkno);
817                 goto bail;
818         }
819         if (status < 0) {
820                 mlog_errno(status);
821                 goto bail;
822         }
823
824         *wipe = 1;
825         mlog(0, "Inode %llu is ok to wipe from orphan dir %u\n",
826              (unsigned long long)oi->ip_blkno,
827              le16_to_cpu(di->i_orphaned_slot));
828
829 bail:
830         return status;
831 }
832
833 /* Support function for ocfs2_delete_inode. Will help us keep the
834  * inode data in a consistent state for clear_inode. Always truncates
835  * pages, optionally sync's them first. */
836 static void ocfs2_cleanup_delete_inode(struct inode *inode,
837                                        int sync_data)
838 {
839         mlog(0, "Cleanup inode %llu, sync = %d\n",
840              (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
841         if (sync_data)
842                 write_inode_now(inode, 1);
843         truncate_inode_pages(&inode->i_data, 0);
844 }
845
846 void ocfs2_delete_inode(struct inode *inode)
847 {
848         int wipe, status;
849         sigset_t blocked, oldset;
850         struct buffer_head *di_bh = NULL;
851
852         mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
853
854         if (is_bad_inode(inode)) {
855                 mlog(0, "Skipping delete of bad inode\n");
856                 goto bail;
857         }
858
859         if (!ocfs2_inode_is_valid_to_delete(inode)) {
860                 /* It's probably not necessary to truncate_inode_pages
861                  * here but we do it for safety anyway (it will most
862                  * likely be a no-op anyway) */
863                 ocfs2_cleanup_delete_inode(inode, 0);
864                 goto bail;
865         }
866
867         /* We want to block signals in delete_inode as the lock and
868          * messaging paths may return us -ERESTARTSYS. Which would
869          * cause us to exit early, resulting in inodes being orphaned
870          * forever. */
871         sigfillset(&blocked);
872         status = sigprocmask(SIG_BLOCK, &blocked, &oldset);
873         if (status < 0) {
874                 mlog_errno(status);
875                 ocfs2_cleanup_delete_inode(inode, 1);
876                 goto bail;
877         }
878
879         /* Lock down the inode. This gives us an up to date view of
880          * it's metadata (for verification), and allows us to
881          * serialize delete_inode votes. 
882          *
883          * Even though we might be doing a truncate, we don't take the
884          * allocation lock here as it won't be needed - nobody will
885          * have the file open.
886          */
887         status = ocfs2_meta_lock(inode, &di_bh, 1);
888         if (status < 0) {
889                 if (status != -ENOENT)
890                         mlog_errno(status);
891                 ocfs2_cleanup_delete_inode(inode, 0);
892                 goto bail_unblock;
893         }
894
895         /* Query the cluster. This will be the final decision made
896          * before we go ahead and wipe the inode. */
897         status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
898         if (!wipe || status < 0) {
899                 /* Error and inode busy vote both mean we won't be
900                  * removing the inode, so they take almost the same
901                  * path. */
902                 if (status < 0)
903                         mlog_errno(status);
904
905                 /* Someone in the cluster has voted to not wipe this
906                  * inode, or it was never completely orphaned. Write
907                  * out the pages and exit now. */
908                 ocfs2_cleanup_delete_inode(inode, 1);
909                 goto bail_unlock_inode;
910         }
911
912         ocfs2_cleanup_delete_inode(inode, 0);
913
914         status = ocfs2_wipe_inode(inode, di_bh);
915         if (status < 0) {
916                 if (status != -EDEADLK)
917                         mlog_errno(status);
918                 goto bail_unlock_inode;
919         }
920
921         /*
922          * Mark the inode as successfully deleted.
923          *
924          * This is important for ocfs2_clear_inode() as it will check
925          * this flag and skip any checkpointing work
926          *
927          * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
928          * the LVB for other nodes.
929          */
930         OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
931
932 bail_unlock_inode:
933         ocfs2_meta_unlock(inode, 1);
934         brelse(di_bh);
935 bail_unblock:
936         status = sigprocmask(SIG_SETMASK, &oldset, NULL);
937         if (status < 0)
938                 mlog_errno(status);
939 bail:
940         clear_inode(inode);
941         mlog_exit_void();
942 }
943
944 void ocfs2_clear_inode(struct inode *inode)
945 {
946         int status;
947         struct ocfs2_inode_info *oi = OCFS2_I(inode);
948
949         mlog_entry_void();
950
951         if (!inode)
952                 goto bail;
953
954         mlog(0, "Clearing inode: %llu, nlink = %u\n",
955              (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
956
957         mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
958                         "Inode=%lu\n", inode->i_ino);
959
960         /* For remove delete_inode vote, we hold open lock before,
961          * now it is time to unlock PR and EX open locks. */
962         ocfs2_open_unlock(inode);
963
964         /* Do these before all the other work so that we don't bounce
965          * the vote thread while waiting to destroy the locks. */
966         ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
967         ocfs2_mark_lockres_freeing(&oi->ip_meta_lockres);
968         ocfs2_mark_lockres_freeing(&oi->ip_data_lockres);
969         ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
970
971         /* We very well may get a clear_inode before all an inodes
972          * metadata has hit disk. Of course, we can't drop any cluster
973          * locks until the journal has finished with it. The only
974          * exception here are successfully wiped inodes - their
975          * metadata can now be considered to be part of the system
976          * inodes from which it came. */
977         if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
978                 ocfs2_checkpoint_inode(inode);
979
980         mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
981                         "Clear inode of %llu, inode has io markers\n",
982                         (unsigned long long)oi->ip_blkno);
983
984         status = ocfs2_drop_inode_locks(inode);
985         if (status < 0)
986                 mlog_errno(status);
987
988         ocfs2_lock_res_free(&oi->ip_rw_lockres);
989         ocfs2_lock_res_free(&oi->ip_meta_lockres);
990         ocfs2_lock_res_free(&oi->ip_data_lockres);
991         ocfs2_lock_res_free(&oi->ip_open_lockres);
992
993         ocfs2_metadata_cache_purge(inode);
994
995         mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached,
996                         "Clear inode of %llu, inode has %u cache items\n",
997                         (unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached);
998
999         mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
1000                         "Clear inode of %llu, inode has a bad flag\n",
1001                         (unsigned long long)oi->ip_blkno);
1002
1003         mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
1004                         "Clear inode of %llu, inode is locked\n",
1005                         (unsigned long long)oi->ip_blkno);
1006
1007         mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
1008                         "Clear inode of %llu, io_mutex is locked\n",
1009                         (unsigned long long)oi->ip_blkno);
1010         mutex_unlock(&oi->ip_io_mutex);
1011
1012         /*
1013          * down_trylock() returns 0, down_write_trylock() returns 1
1014          * kernel 1, world 0
1015          */
1016         mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
1017                         "Clear inode of %llu, alloc_sem is locked\n",
1018                         (unsigned long long)oi->ip_blkno);
1019         up_write(&oi->ip_alloc_sem);
1020
1021         mlog_bug_on_msg(oi->ip_open_count,
1022                         "Clear inode of %llu has open count %d\n",
1023                         (unsigned long long)oi->ip_blkno, oi->ip_open_count);
1024
1025         /* Clear all other flags. */
1026         oi->ip_flags = OCFS2_INODE_CACHE_INLINE;
1027         oi->ip_created_trans = 0;
1028         oi->ip_last_trans = 0;
1029         oi->ip_dir_start_lookup = 0;
1030         oi->ip_blkno = 0ULL;
1031
1032 bail:
1033         mlog_exit_void();
1034 }
1035
1036 /* Called under inode_lock, with no more references on the
1037  * struct inode, so it's safe here to check the flags field
1038  * and to manipulate i_nlink without any other locks. */
1039 void ocfs2_drop_inode(struct inode *inode)
1040 {
1041         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1042
1043         mlog_entry_void();
1044
1045         mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n",
1046              (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags);
1047
1048         if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
1049                 generic_delete_inode(inode);
1050         else
1051                 generic_drop_inode(inode);
1052
1053         mlog_exit_void();
1054 }
1055
1056 /*
1057  * TODO: this should probably be merged into ocfs2_get_block
1058  *
1059  * However, you now need to pay attention to the cont_prepare_write()
1060  * stuff in ocfs2_get_block (that is, ocfs2_get_block pretty much
1061  * expects never to extend).
1062  */
1063 struct buffer_head *ocfs2_bread(struct inode *inode,
1064                                 int block, int *err, int reada)
1065 {
1066         struct buffer_head *bh = NULL;
1067         int tmperr;
1068         u64 p_blkno;
1069         int readflags = OCFS2_BH_CACHED;
1070
1071         if (reada)
1072                 readflags |= OCFS2_BH_READAHEAD;
1073
1074         if (((u64)block << inode->i_sb->s_blocksize_bits) >=
1075             i_size_read(inode)) {
1076                 BUG_ON(!reada);
1077                 return NULL;
1078         }
1079
1080         tmperr = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL);
1081         if (tmperr < 0) {
1082                 mlog_errno(tmperr);
1083                 goto fail;
1084         }
1085
1086         tmperr = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno, &bh,
1087                                   readflags, inode);
1088         if (tmperr < 0)
1089                 goto fail;
1090
1091         tmperr = 0;
1092
1093         *err = 0;
1094         return bh;
1095
1096 fail:
1097         if (bh) {
1098                 brelse(bh);
1099                 bh = NULL;
1100         }
1101         *err = -EIO;
1102         return NULL;
1103 }
1104
1105 /*
1106  * This is called from our getattr.
1107  */
1108 int ocfs2_inode_revalidate(struct dentry *dentry)
1109 {
1110         struct inode *inode = dentry->d_inode;
1111         int status = 0;
1112
1113         mlog_entry("(inode = 0x%p, ino = %llu)\n", inode,
1114                    inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
1115
1116         if (!inode) {
1117                 mlog(0, "eep, no inode!\n");
1118                 status = -ENOENT;
1119                 goto bail;
1120         }
1121
1122         spin_lock(&OCFS2_I(inode)->ip_lock);
1123         if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
1124                 spin_unlock(&OCFS2_I(inode)->ip_lock);
1125                 mlog(0, "inode deleted!\n");
1126                 status = -ENOENT;
1127                 goto bail;
1128         }
1129         spin_unlock(&OCFS2_I(inode)->ip_lock);
1130
1131         /* Let ocfs2_meta_lock do the work of updating our struct
1132          * inode for us. */
1133         status = ocfs2_meta_lock(inode, NULL, 0);
1134         if (status < 0) {
1135                 if (status != -ENOENT)
1136                         mlog_errno(status);
1137                 goto bail;
1138         }
1139         ocfs2_meta_unlock(inode, 0);
1140 bail:
1141         mlog_exit(status);
1142
1143         return status;
1144 }
1145
1146 /*
1147  * Updates a disk inode from a
1148  * struct inode.
1149  * Only takes ip_lock.
1150  */
1151 int ocfs2_mark_inode_dirty(handle_t *handle,
1152                            struct inode *inode,
1153                            struct buffer_head *bh)
1154 {
1155         int status;
1156         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
1157
1158         mlog_entry("(inode %llu)\n",
1159                    (unsigned long long)OCFS2_I(inode)->ip_blkno);
1160
1161         status = ocfs2_journal_access(handle, inode, bh,
1162                                       OCFS2_JOURNAL_ACCESS_WRITE);
1163         if (status < 0) {
1164                 mlog_errno(status);
1165                 goto leave;
1166         }
1167
1168         spin_lock(&OCFS2_I(inode)->ip_lock);
1169         fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
1170         fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
1171         spin_unlock(&OCFS2_I(inode)->ip_lock);
1172
1173         fe->i_size = cpu_to_le64(i_size_read(inode));
1174         fe->i_links_count = cpu_to_le16(inode->i_nlink);
1175         fe->i_uid = cpu_to_le32(inode->i_uid);
1176         fe->i_gid = cpu_to_le32(inode->i_gid);
1177         fe->i_mode = cpu_to_le16(inode->i_mode);
1178         fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
1179         fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
1180         fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1181         fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1182         fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
1183         fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
1184
1185         status = ocfs2_journal_dirty(handle, bh);
1186         if (status < 0)
1187                 mlog_errno(status);
1188
1189         status = 0;
1190 leave:
1191
1192         mlog_exit(status);
1193         return status;
1194 }
1195
1196 /*
1197  *
1198  * Updates a struct inode from a disk inode.
1199  * does no i/o, only takes ip_lock.
1200  */
1201 void ocfs2_refresh_inode(struct inode *inode,
1202                          struct ocfs2_dinode *fe)
1203 {
1204         spin_lock(&OCFS2_I(inode)->ip_lock);
1205
1206         OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1207         OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
1208         ocfs2_set_inode_flags(inode);
1209         i_size_write(inode, le64_to_cpu(fe->i_size));
1210         inode->i_nlink = le16_to_cpu(fe->i_links_count);
1211         inode->i_uid = le32_to_cpu(fe->i_uid);
1212         inode->i_gid = le32_to_cpu(fe->i_gid);
1213         inode->i_mode = le16_to_cpu(fe->i_mode);
1214         if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1215                 inode->i_blocks = 0;
1216         else
1217                 inode->i_blocks = ocfs2_align_bytes_to_sectors(i_size_read(inode));
1218         inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
1219         inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
1220         inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
1221         inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
1222         inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
1223         inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
1224
1225         spin_unlock(&OCFS2_I(inode)->ip_lock);
1226 }