nfsd: move most of nfsfh.h to fs/nfsd
[safe/jmp/linux-2.6] / fs / ubifs / journal.c
index 3bc3fc9..d321bae 100644 (file)
@@ -114,7 +114,7 @@ static inline void zero_trun_node_unused(struct ubifs_trun_node *trun)
  */
 static int reserve_space(struct ubifs_info *c, int jhead, int len)
 {
-       int err = 0, err1, retries = 0, avail, lnum, offs, free, squeeze;
+       int err = 0, err1, retries = 0, avail, lnum, offs, squeeze;
        struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
 
        /*
@@ -139,10 +139,9 @@ again:
         * Write buffer wasn't seek'ed or there is no enough space - look for an
         * LEB with some empty space.
         */
-       lnum = ubifs_find_free_space(c, len, &free, squeeze);
+       lnum = ubifs_find_free_space(c, len, &offs, squeeze);
        if (lnum >= 0) {
                /* Found an LEB, add it to the journal head */
-               offs = c->leb_size - free;
                err = ubifs_add_bud_to_log(c, jhead, lnum, offs);
                if (err)
                        goto out_return;
@@ -159,7 +158,7 @@ again:
         * some. But the write-buffer mutex has to be unlocked because
         * GC also takes it.
         */
-       dbg_jnl("no free space  jhead %d, run GC", jhead);
+       dbg_jnl("no free space in jhead %s, run GC", dbg_jhead(jhead));
        mutex_unlock(&wbuf->io_mutex);
 
        lnum = ubifs_garbage_collect(c, 0);
@@ -174,7 +173,8 @@ again:
                 * because we dropped @wbuf->io_mutex, so try once
                 * again.
                 */
-               dbg_jnl("GC couldn't make a free LEB for jhead %d", jhead);
+               dbg_jnl("GC couldn't make a free LEB for jhead %s",
+                       dbg_jhead(jhead));
                if (retries++ < 2) {
                        dbg_jnl("retry (%d)", retries);
                        goto again;
@@ -185,13 +185,13 @@ again:
        }
 
        mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
-       dbg_jnl("got LEB %d for jhead %d", lnum, jhead);
+       dbg_jnl("got LEB %d for jhead %s", lnum, dbg_jhead(jhead));
        avail = c->leb_size - wbuf->offs - wbuf->used;
 
        if (wbuf->lnum != -1 && avail >= len) {
                /*
                 * Someone else has switched the journal head and we have
-                * enough space now. This happens when more then one process is
+                * enough space now. This happens when more than one process is
                 * trying to write to the same journal head at the same time.
                 */
                dbg_jnl("return LEB %d back, already have LEB %d:%d",
@@ -208,7 +208,7 @@ again:
        offs = 0;
 
 out:
-       err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs, UBI_SHORTTERM);
+       err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs, wbuf->dtype);
        if (err)
                goto out_unlock;
 
@@ -256,7 +256,8 @@ static int write_node(struct ubifs_info *c, int jhead, void *node, int len,
        *lnum = c->jheads[jhead].wbuf.lnum;
        *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
 
-       dbg_jnl("jhead %d, LEB %d:%d, len %d", jhead, *lnum, *offs, len);
+       dbg_jnl("jhead %s, LEB %d:%d, len %d",
+               dbg_jhead(jhead), *lnum, *offs, len);
        ubifs_prepare_node(c, node, len, 0);
 
        return ubifs_wbuf_write_nolock(wbuf, node, len);
@@ -286,7 +287,8 @@ static int write_head(struct ubifs_info *c, int jhead, void *buf, int len,
 
        *lnum = c->jheads[jhead].wbuf.lnum;
        *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
-       dbg_jnl("jhead %d, LEB %d:%d, len %d", jhead, *lnum, *offs, len);
+       dbg_jnl("jhead %s, LEB %d:%d, len %d",
+               dbg_jhead(jhead), *lnum, *offs, len);
 
        err = ubifs_wbuf_write_nolock(wbuf, buf, len);
        if (err)
@@ -604,6 +606,7 @@ int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
                        release_head(c, BASEHD);
                        goto out_finish;
                }
+               ui->del_cmtno = c->cmt_no;
        }
 
        err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
@@ -689,8 +692,9 @@ int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
        int dlen = UBIFS_DATA_NODE_SZ + UBIFS_BLOCK_SIZE * WORST_COMPR_FACTOR;
        struct ubifs_inode *ui = ubifs_inode(inode);
 
-       dbg_jnl("ino %lu, blk %u, len %d, key %s", key_inum(c, key),
-               key_block(c, key), len, DBGKEY(key));
+       dbg_jnl("ino %lu, blk %u, len %d, key %s",
+               (unsigned long)key_inum(c, key), key_block(c, key), len,
+               DBGKEY(key));
        ubifs_assert(len <= UBIFS_BLOCK_SIZE);
 
        data = kmalloc(dlen, GFP_NOFS);
@@ -702,7 +706,7 @@ int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
        data->size = cpu_to_le32(len);
        zero_data_node_unused(data);
 
-       if (!(ui->flags && UBIFS_COMPR_FL))
+       if (!(ui->flags & UBIFS_COMPR_FL))
                /* Compression is disabled for this inode */
                compr_type = UBIFS_COMPR_NONE;
        else
@@ -821,6 +825,65 @@ out_free:
 }
 
 /**
+ * ubifs_jnl_delete_inode - delete an inode.
+ * @c: UBIFS file-system description object
+ * @inode: inode to delete
+ *
+ * This function deletes inode @inode which includes removing it from orphans,
+ * deleting it from TNC and, in some cases, writing a deletion inode to the
+ * journal.
+ *
+ * When regular file inodes are unlinked or a directory inode is removed, the
+ * 'ubifs_jnl_update()' function writes a corresponding deletion inode and
+ * direntry to the media, and adds the inode to orphans. After this, when the
+ * last reference to this inode has been dropped, this function is called. In
+ * general, it has to write one more deletion inode to the media, because if
+ * a commit happened between 'ubifs_jnl_update()' and
+ * 'ubifs_jnl_delete_inode()', the deletion inode is not in the journal
+ * anymore, and in fact it might not be on the flash anymore, because it might
+ * have been garbage-collected already. And for optimization reasons UBIFS does
+ * not read the orphan area if it has been unmounted cleanly, so it would have
+ * no indication in the journal that there is a deleted inode which has to be
+ * removed from TNC.
+ *
+ * However, if there was no commit between 'ubifs_jnl_update()' and
+ * 'ubifs_jnl_delete_inode()', then there is no need to write the deletion
+ * inode to the media for the second time. And this is quite a typical case.
+ *
+ * This function returns zero in case of success and a negative error code in
+ * case of failure.
+ */
+int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode)
+{
+       int err;
+       struct ubifs_inode *ui = ubifs_inode(inode);
+
+       ubifs_assert(inode->i_nlink == 0);
+
+       if (ui->del_cmtno != c->cmt_no)
+               /* A commit happened for sure */
+               return ubifs_jnl_write_inode(c, inode);
+
+       down_read(&c->commit_sem);
+       /*
+        * Check commit number again, because the first test has been done
+        * without @c->commit_sem, so a commit might have happened.
+        */
+       if (ui->del_cmtno != c->cmt_no) {
+               up_read(&c->commit_sem);
+               return ubifs_jnl_write_inode(c, inode);
+       }
+
+       err = ubifs_tnc_remove_ino(c, inode->i_ino);
+       if (err)
+               ubifs_ro_mode(c, err);
+       else
+               ubifs_delete_orphan(c, inode->i_ino);
+       up_read(&c->commit_sem);
+       return err;
+}
+
+/**
  * ubifs_jnl_rename - rename a directory entry.
  * @c: UBIFS file-system description object
  * @old_dir: parent inode of directory entry to rename
@@ -928,6 +991,7 @@ int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
                        release_head(c, BASEHD);
                        goto out_finish;
                }
+               new_ui->del_cmtno = c->cmt_no;
        }
 
        err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
@@ -1067,7 +1131,8 @@ int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
        ino_t inum = inode->i_ino;
        unsigned int blk;
 
-       dbg_jnl("ino %lu, size %lld -> %lld", inum, old_size, new_size);
+       dbg_jnl("ino %lu, size %lld -> %lld",
+               (unsigned long)inum, old_size, new_size);
        ubifs_assert(!ui->data_len);
        ubifs_assert(S_ISREG(inode->i_mode));
        ubifs_assert(mutex_is_locked(&ui->ui_mutex));
@@ -1157,7 +1222,7 @@ int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
        data_key_init(c, &key, inum, blk);
 
        bit = old_size & (UBIFS_BLOCK_SIZE - 1);
-       blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0: 1);
+       blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0 : 1);
        data_key_init(c, &to_key, inum, blk);
 
        err = ubifs_tnc_remove_range(c, &key, &to_key);
@@ -1303,7 +1368,7 @@ out_ro:
  * @host: host inode
  *
  * This function writes the updated version of an extended attribute inode and
- * the host inode tho the journal (to the base head). The host inode is written
+ * the host inode to the journal (to the base head). The host inode is written
  * after the extended attribute inode in order to guarantee that the extended
  * attribute will be flushed when the inode is synchronized by 'fsync()' and
  * consequently, the write-buffer is synchronized. This function returns zero
@@ -1313,7 +1378,7 @@ int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
                           const struct inode *host)
 {
        int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
-       struct ubifs_inode *host_ui = ubifs_inode(inode);
+       struct ubifs_inode *host_ui = ubifs_inode(host);
        struct ubifs_ino_node *ino;
        union ubifs_key key;
        int sync = IS_DIRSYNC(host);