^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2) * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 5) #include <linux/time.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 6) #include <linux/fs.h>
f466c6fdb3b1f (Al Viro 2012-03-17 01:16:43 -0400 7) #include "reiserfs.h"
a3063ab88fcbe (Al Viro 2012-03-17 01:03:10 -0400 8) #include "acl.h"
c45ac8887e778 (Al Viro 2012-03-17 00:59:06 -0400 9) #include "xattr.h"
a569425512253 (Christoph Hellwig 2007-07-17 04:04:28 -0700 10) #include <linux/exportfs.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 11) #include <linux/pagemap.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 12) #include <linux/highmem.h>
5a0e3ad6af866 (Tejun Heo 2010-03-24 17:04:11 +0900 13) #include <linux/slab.h>
17093991af499 (Fabian Frederick 2014-08-08 14:21:12 -0700 14) #include <linux/uaccess.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 15) #include <asm/unaligned.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 16) #include <linux/buffer_head.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 17) #include <linux/mpage.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 18) #include <linux/writeback.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 19) #include <linux/quotaops.h>
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 20) #include <linux/swap.h>
e2e40f2c1ed43 (Christoph Hellwig 2015-02-22 08:58:50 -0800 21) #include <linux/uio.h>
be297968da22c (Christoph Hellwig 2016-11-01 07:40:16 -0600 22) #include <linux/bio.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 23)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 24) int reiserfs_commit_write(struct file *f, struct page *page,
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 25) unsigned from, unsigned to);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 26)
845a2cc050705 (Al Viro 2010-06-07 11:37:37 -0400 27) void reiserfs_evict_inode(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 28) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 29) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 30) * We need blocks for transaction + (user+group) quota
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 31) * update (possibly delete)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 32) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 33) int jbegin_count =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 34) JOURNAL_PER_BALANCE_CNT * 2 +
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 35) 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 36) struct reiserfs_transaction_handle th;
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 37) int err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 38)
845a2cc050705 (Al Viro 2010-06-07 11:37:37 -0400 39) if (!inode->i_nlink && !is_bad_inode(inode))
871a293155a24 (Christoph Hellwig 2010-03-03 09:05:07 -0500 40) dquot_initialize(inode);
907f4554e2521 (Christoph Hellwig 2010-03-03 09:05:06 -0500 41)
91b0abe36a7b2 (Johannes Weiner 2014-04-03 14:47:49 -0700 42) truncate_inode_pages_final(&inode->i_data);
845a2cc050705 (Al Viro 2010-06-07 11:37:37 -0400 43) if (inode->i_nlink)
845a2cc050705 (Al Viro 2010-06-07 11:37:37 -0400 44) goto no_delete;
fef266580e5cf (Mark Fasheh 2005-09-09 13:01:31 -0700 45)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 46) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 47) * The = 0 happens when we abort creating a new inode
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 48) * for some reason like lack of space..
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 49) * also handles bad_inode case
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 50) */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 51) if (!(inode->i_state & I_NEW) && INODE_PKEY(inode)->k_objectid != 0) {
4c05141df57f4 (Jeff Mahoney 2013-08-08 17:27:19 -0400 52)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 53) reiserfs_delete_xattrs(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 54)
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 55) reiserfs_write_lock(inode->i_sb);
4c05141df57f4 (Jeff Mahoney 2013-08-08 17:27:19 -0400 56)
b0b33dee2dcc8 (Alexander Zarochentsev 2006-08-05 12:14:01 -0700 57) if (journal_begin(&th, inode->i_sb, jbegin_count))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 58) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 59) reiserfs_update_inode_transaction(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 60)
eb35c218d83ec (Jeff Mahoney 2008-07-08 14:37:06 -0400 61) reiserfs_discard_prealloc(&th, inode);
eb35c218d83ec (Jeff Mahoney 2008-07-08 14:37:06 -0400 62)
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 63) err = reiserfs_delete_object(&th, inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 64)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 65) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 66) * Do quota update inside a transaction for journaled quotas.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 67) * We must do that after delete_object so that quota updates
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 68) * go into the same transaction as stat data deletion
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 69) */
d2d0395fd1778 (Jeff Mahoney 2013-08-08 17:34:47 -0400 70) if (!err) {
d2d0395fd1778 (Jeff Mahoney 2013-08-08 17:34:47 -0400 71) int depth = reiserfs_write_unlock_nested(inode->i_sb);
63936ddaa16b9 (Christoph Hellwig 2010-03-03 09:05:01 -0500 72) dquot_free_inode(inode);
d2d0395fd1778 (Jeff Mahoney 2013-08-08 17:34:47 -0400 73) reiserfs_write_lock_nested(inode->i_sb, depth);
d2d0395fd1778 (Jeff Mahoney 2013-08-08 17:34:47 -0400 74) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 75)
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 76) if (journal_end(&th))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 77) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 78)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 79) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 80) * check return value from reiserfs_delete_object after
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 81) * ending the transaction
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 82) */
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 83) if (err)
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 84) goto out;
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 85)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 86) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 87) * all items of file are deleted, so we can remove
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 88) * "save" link
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 89) * we can't do anything about an error here
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 90) */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 91) remove_save_link(inode, 0 /* not truncate */);
4c05141df57f4 (Jeff Mahoney 2013-08-08 17:27:19 -0400 92) out:
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 93) reiserfs_write_unlock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 94) } else {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 95) /* no object items are in the tree */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 96) ;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 97) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 98)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 99) /* note this must go after the journal_end to prevent deadlock */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 100) clear_inode(inode);
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 101)
845a2cc050705 (Al Viro 2010-06-07 11:37:37 -0400 102) dquot_drop(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 103) inode->i_blocks = 0;
f4ae2faa40199 (Sergey Senozhatsky 2010-08-11 14:07:01 +0300 104) return;
845a2cc050705 (Al Viro 2010-06-07 11:37:37 -0400 105)
845a2cc050705 (Al Viro 2010-06-07 11:37:37 -0400 106) no_delete:
dbd5768f87ff6 (Jan Kara 2012-05-03 14:48:02 +0200 107) clear_inode(inode);
845a2cc050705 (Al Viro 2010-06-07 11:37:37 -0400 108) dquot_drop(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 109) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 110)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 111) static void _make_cpu_key(struct cpu_key *key, int version, __u32 dirid,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 112) __u32 objectid, loff_t offset, int type, int length)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 113) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 114) key->version = version;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 115)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 116) key->on_disk_key.k_dir_id = dirid;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 117) key->on_disk_key.k_objectid = objectid;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 118) set_cpu_key_k_offset(key, offset);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 119) set_cpu_key_k_type(key, type);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 120) key->key_length = length;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 121) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 122)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 123) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 124) * take base of inode_key (it comes from inode always) (dirid, objectid)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 125) * and version from an inode, set offset and type of key
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 126) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 127) void make_cpu_key(struct cpu_key *key, struct inode *inode, loff_t offset,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 128) int type, int length)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 129) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 130) _make_cpu_key(key, get_inode_item_key_version(inode),
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 131) le32_to_cpu(INODE_PKEY(inode)->k_dir_id),
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 132) le32_to_cpu(INODE_PKEY(inode)->k_objectid), offset, type,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 133) length);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 134) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 135)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 136) /* when key is 0, do not set version and short key */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 137) inline void make_le_item_head(struct item_head *ih, const struct cpu_key *key,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 138) int version,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 139) loff_t offset, int type, int length,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 140) int entry_count /*or ih_free_space */ )
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 141) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 142) if (key) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 143) ih->ih_key.k_dir_id = cpu_to_le32(key->on_disk_key.k_dir_id);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 144) ih->ih_key.k_objectid =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 145) cpu_to_le32(key->on_disk_key.k_objectid);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 146) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 147) put_ih_version(ih, version);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 148) set_le_ih_k_offset(ih, offset);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 149) set_le_ih_k_type(ih, type);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 150) put_ih_item_len(ih, length);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 151) /* set_ih_free_space (ih, 0); */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 152) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 153) * for directory items it is entry count, for directs and stat
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 154) * datas - 0xffff, for indirects - 0
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 155) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 156) put_ih_entry_count(ih, entry_count);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 157) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 158)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 159) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 160) * FIXME: we might cache recently accessed indirect item
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 161) * Ugh. Not too eager for that....
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 162) * I cut the code until such time as I see a convincing argument (benchmark).
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 163) * I don't want a bloated inode struct..., and I don't like code complexity....
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 164) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 165)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 166) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 167) * cutting the code is fine, since it really isn't in use yet and is easy
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 168) * to add back in. But, Vladimir has a really good idea here. Think
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 169) * about what happens for reading a file. For each page,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 170) * The VFS layer calls reiserfs_readpage, who searches the tree to find
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 171) * an indirect item. This indirect item has X number of pointers, where
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 172) * X is a big number if we've done the block allocation right. But,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 173) * we only use one or two of these pointers during each call to readpage,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 174) * needlessly researching again later on.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 175) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 176) * The size of the cache could be dynamic based on the size of the file.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 177) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 178) * I'd also like to see us cache the location the stat data item, since
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 179) * we are needlessly researching for that frequently.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 180) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 181) * --chris
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 182) */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 183)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 184) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 185) * If this page has a file tail in it, and
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 186) * it was read in by get_block_create_0, the page data is valid,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 187) * but tail is still sitting in a direct item, and we can't write to
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 188) * it. So, look through this page, and check all the mapped buffers
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 189) * to make sure they have valid block numbers. Any that don't need
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 190) * to be unmapped, so that __block_write_begin will correctly call
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 191) * reiserfs_get_block to convert the tail into an unformatted node
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 192) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 193) static inline void fix_tail_page_for_writing(struct page *page)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 194) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 195) struct buffer_head *head, *next, *bh;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 196)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 197) if (page && page_has_buffers(page)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 198) head = page_buffers(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 199) bh = head;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 200) do {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 201) next = bh->b_this_page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 202) if (buffer_mapped(bh) && bh->b_blocknr == 0) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 203) reiserfs_unmap_buffer(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 204) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 205) bh = next;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 206) } while (bh != head);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 207) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 208) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 209)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 210) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 211) * reiserfs_get_block does not need to allocate a block only if it has been
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 212) * done already or non-hole position has been found in the indirect item
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 213) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 214) static inline int allocation_needed(int retval, b_blocknr_t allocated,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 215) struct item_head *ih,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 216) __le32 * item, int pos_in_item)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 217) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 218) if (allocated)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 219) return 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 220) if (retval == POSITION_FOUND && is_indirect_le_ih(ih) &&
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 221) get_block_num(item, pos_in_item))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 222) return 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 223) return 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 224) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 225)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 226) static inline int indirect_item_found(int retval, struct item_head *ih)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 227) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 228) return (retval == POSITION_FOUND) && is_indirect_le_ih(ih);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 229) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 230)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 231) static inline void set_block_dev_mapped(struct buffer_head *bh,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 232) b_blocknr_t block, struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 233) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 234) map_bh(bh, inode->i_sb, block);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 235) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 236)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 237) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 238) * files which were created in the earlier version can not be longer,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 239) * than 2 gb
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 240) */
3ee1667042c35 (Jeff Mahoney 2007-10-18 23:39:25 -0700 241) static int file_capable(struct inode *inode, sector_t block)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 242) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 243) /* it is new file. */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 244) if (get_inode_item_key_version(inode) != KEY_FORMAT_3_5 ||
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 245) /* old file, but 'block' is inside of 2gb */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 246) block < (1 << (31 - inode->i_sb->s_blocksize_bits)))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 247) return 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 248)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 249) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 250) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 251)
deba0f49b9345 (Adrian Bunk 2007-10-16 23:26:03 -0700 252) static int restart_transaction(struct reiserfs_transaction_handle *th,
deba0f49b9345 (Adrian Bunk 2007-10-16 23:26:03 -0700 253) struct inode *inode, struct treepath *path)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 254) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 255) struct super_block *s = th->t_super;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 256) int err;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 257)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 258) BUG_ON(!th->t_trans_id);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 259) BUG_ON(!th->t_refcount);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 260)
87b4126f10cce (Suzuki K P 2006-12-06 20:36:10 -0800 261) pathrelse(path);
87b4126f10cce (Suzuki K P 2006-12-06 20:36:10 -0800 262)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 263) /* we cannot restart while nested */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 264) if (th->t_refcount > 1) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 265) return 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 266) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 267) reiserfs_update_sd(th, inode);
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 268) err = journal_end(th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 269) if (!err) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 270) err = journal_begin(th, s, JOURNAL_PER_BALANCE_CNT * 6);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 271) if (!err)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 272) reiserfs_update_inode_transaction(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 273) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 274) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 275) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 276)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 277) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 278) * it is called by get_block when create == 0. Returns block number
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 279) * for 'block'-th logical block of file. When it hits direct item it
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 280) * returns 0 (being called from bmap) or read direct item into piece
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 281) * of page (bh_result)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 282) * Please improve the english/clarity in the comment above, as it is
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 283) * hard to understand.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 284) */
3ee1667042c35 (Jeff Mahoney 2007-10-18 23:39:25 -0700 285) static int _get_block_create_0(struct inode *inode, sector_t block,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 286) struct buffer_head *bh_result, int args)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 287) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 288) INITIALIZE_PATH(path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 289) struct cpu_key key;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 290) struct buffer_head *bh;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 291) struct item_head *ih, tmp_ih;
3ee1667042c35 (Jeff Mahoney 2007-10-18 23:39:25 -0700 292) b_blocknr_t blocknr;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 293) char *p = NULL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 294) int chars;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 295) int ret;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 296) int result;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 297) int done = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 298) unsigned long offset;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 299)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 300) /* prepare the key to look for the 'block'-th block of file */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 301) make_cpu_key(&key, inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 302) (loff_t) block * inode->i_sb->s_blocksize + 1, TYPE_ANY,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 303) 3);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 304)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 305) result = search_for_position_by_key(inode->i_sb, &key, &path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 306) if (result != POSITION_FOUND) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 307) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 308) if (p)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 309) kunmap(bh_result->b_page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 310) if (result == IO_ERROR)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 311) return -EIO;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 312) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 313) * We do not return -ENOENT if there is a hole but page is
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 314) * uptodate, because it means that there is some MMAPED data
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 315) * associated with it that is yet to be written to disk.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 316) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 317) if ((args & GET_BLOCK_NO_HOLE)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 318) && !PageUptodate(bh_result->b_page)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 319) return -ENOENT;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 320) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 321) return 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 322) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 323)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 324) bh = get_last_bh(&path);
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 325) ih = tp_item_head(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 326) if (is_indirect_le_ih(ih)) {
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 327) __le32 *ind_item = (__le32 *) ih_item_body(bh, ih);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 328)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 329) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 330) * FIXME: here we could cache indirect item or part of it in
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 331) * the inode to avoid search_by_key in case of subsequent
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 332) * access to file
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 333) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 334) blocknr = get_block_num(ind_item, path.pos_in_item);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 335) ret = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 336) if (blocknr) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 337) map_bh(bh_result, inode->i_sb, blocknr);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 338) if (path.pos_in_item ==
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 339) ((ih_item_len(ih) / UNFM_P_SIZE) - 1)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 340) set_buffer_boundary(bh_result);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 341) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 342) } else
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 343) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 344) * We do not return -ENOENT if there is a hole but
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 345) * page is uptodate, because it means that there is
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 346) * some MMAPED data associated with it that is
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 347) * yet to be written to disk.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 348) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 349) if ((args & GET_BLOCK_NO_HOLE)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 350) && !PageUptodate(bh_result->b_page)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 351) ret = -ENOENT;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 352) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 353)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 354) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 355) if (p)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 356) kunmap(bh_result->b_page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 357) return ret;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 358) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 359) /* requested data are in direct item(s) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 360) if (!(args & GET_BLOCK_READ_DIRECT)) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 361) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 362) * we are called by bmap. FIXME: we can not map block of file
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 363) * when it is stored in direct item(s)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 364) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 365) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 366) if (p)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 367) kunmap(bh_result->b_page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 368) return -ENOENT;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 369) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 370)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 371) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 372) * if we've got a direct item, and the buffer or page was uptodate,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 373) * we don't want to pull data off disk again. skip to the
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 374) * end, where we map the buffer and return
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 375) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 376) if (buffer_uptodate(bh_result)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 377) goto finished;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 378) } else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 379) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 380) * grab_tail_page can trigger calls to reiserfs_get_block on
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 381) * up to date pages without any buffers. If the page is up
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 382) * to date, we don't want read old data off disk. Set the up
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 383) * to date bit on the buffer instead and jump to the end
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 384) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 385) if (!bh_result->b_page || PageUptodate(bh_result->b_page)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 386) set_buffer_uptodate(bh_result);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 387) goto finished;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 388) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 389) /* read file tail into part of page */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 390) offset = (cpu_key_k_offset(&key) - 1) & (PAGE_SIZE - 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 391) copy_item_head(&tmp_ih, ih);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 392)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 393) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 394) * we only want to kmap if we are reading the tail into the page.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 395) * this is not the common case, so we don't kmap until we are
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 396) * sure we need to. But, this means the item might move if
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 397) * kmap schedules
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 398) */
27b3a5c51b50a (Frederic Weisbecker 2009-10-14 23:34:31 +0200 399) if (!p)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 400) p = (char *)kmap(bh_result->b_page);
27b3a5c51b50a (Frederic Weisbecker 2009-10-14 23:34:31 +0200 401)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 402) p += offset;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 403) memset(p, 0, inode->i_sb->s_blocksize);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 404) do {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 405) if (!is_direct_le_ih(ih)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 406) BUG();
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 407) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 408) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 409) * make sure we don't read more bytes than actually exist in
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 410) * the file. This can happen in odd cases where i_size isn't
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 411) * correct, and when direct item padding results in a few
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 412) * extra bytes at the end of the direct item
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 413) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 414) if ((le_ih_k_offset(ih) + path.pos_in_item) > inode->i_size)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 415) break;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 416) if ((le_ih_k_offset(ih) - 1 + ih_item_len(ih)) > inode->i_size) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 417) chars =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 418) inode->i_size - (le_ih_k_offset(ih) - 1) -
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 419) path.pos_in_item;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 420) done = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 421) } else {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 422) chars = ih_item_len(ih) - path.pos_in_item;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 423) }
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 424) memcpy(p, ih_item_body(bh, ih) + path.pos_in_item, chars);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 425)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 426) if (done)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 427) break;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 428)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 429) p += chars;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 430)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 431) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 432) * we done, if read direct item is not the last item of
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 433) * node FIXME: we could try to check right delimiting key
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 434) * to see whether direct item continues in the right
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 435) * neighbor or rely on i_size
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 436) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 437) if (PATH_LAST_POSITION(&path) != (B_NR_ITEMS(bh) - 1))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 438) break;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 439)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 440) /* update key to look for the next piece */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 441) set_cpu_key_k_offset(&key, cpu_key_k_offset(&key) + chars);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 442) result = search_for_position_by_key(inode->i_sb, &key, &path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 443) if (result != POSITION_FOUND)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 444) /* i/o error most likely */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 445) break;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 446) bh = get_last_bh(&path);
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 447) ih = tp_item_head(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 448) } while (1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 449)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 450) flush_dcache_page(bh_result->b_page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 451) kunmap(bh_result->b_page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 452)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 453) finished:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 454) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 455)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 456) if (result == IO_ERROR)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 457) return -EIO;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 458)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 459) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 460) * this buffer has valid data, but isn't valid for io. mapping it to
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 461) * block #0 tells the rest of reiserfs it just has a tail in it
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 462) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 463) map_bh(bh_result, inode->i_sb, 0);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 464) set_buffer_uptodate(bh_result);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 465) return 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 466) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 467)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 468) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 469) * this is called to create file map. So, _get_block_create_0 will not
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 470) * read direct item
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 471) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 472) static int reiserfs_bmap(struct inode *inode, sector_t block,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 473) struct buffer_head *bh_result, int create)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 474) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 475) if (!file_capable(inode, block))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 476) return -EFBIG;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 477)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 478) reiserfs_write_lock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 479) /* do not read the direct item */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 480) _get_block_create_0(inode, block, bh_result, 0);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 481) reiserfs_write_unlock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 482) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 483) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 484)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 485) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 486) * special version of get_block that is only used by grab_tail_page right
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 487) * now. It is sent to __block_write_begin, and when you try to get a
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 488) * block past the end of the file (or a block from a hole) it returns
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 489) * -ENOENT instead of a valid buffer. __block_write_begin expects to
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 490) * be able to do i/o on the buffers returned, unless an error value
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 491) * is also returned.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 492) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 493) * So, this allows __block_write_begin to be used for reading a single block
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 494) * in a page. Where it does not produce a valid page for holes, or past the
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 495) * end of the file. This turns out to be exactly what we need for reading
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 496) * tails for conversion.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 497) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 498) * The point of the wrapper is forcing a certain value for create, even
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 499) * though the VFS layer is calling this function with create==1. If you
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 500) * don't want to send create == GET_BLOCK_NO_HOLE to reiserfs_get_block,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 501) * don't use this function.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 502) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 503) static int reiserfs_get_block_create_0(struct inode *inode, sector_t block,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 504) struct buffer_head *bh_result,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 505) int create)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 506) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 507) return reiserfs_get_block(inode, block, bh_result, GET_BLOCK_NO_HOLE);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 508) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 509)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 510) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 511) * This is special helper for reiserfs_get_block in case we are executing
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 512) * direct_IO request.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 513) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 514) static int reiserfs_get_blocks_direct_io(struct inode *inode,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 515) sector_t iblock,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 516) struct buffer_head *bh_result,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 517) int create)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 518) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 519) int ret;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 520)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 521) bh_result->b_page = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 522)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 523) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 524) * We set the b_size before reiserfs_get_block call since it is
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 525) * referenced in convert_tail_for_hole() that may be called from
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 526) * reiserfs_get_block()
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 527) */
93407472a21b8 (Fabian Frederick 2017-02-27 14:28:32 -0800 528) bh_result->b_size = i_blocksize(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 529)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 530) ret = reiserfs_get_block(inode, iblock, bh_result,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 531) create | GET_BLOCK_NO_DANGLE);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 532) if (ret)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 533) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 534)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 535) /* don't allow direct io onto tail pages */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 536) if (buffer_mapped(bh_result) && bh_result->b_blocknr == 0) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 537) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 538) * make sure future calls to the direct io funcs for this
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 539) * offset in the file fail by unmapping the buffer
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 540) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 541) clear_buffer_mapped(bh_result);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 542) ret = -EINVAL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 543) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 544)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 545) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 546) * Possible unpacked tail. Flush the data before pages have
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 547) * disappeared
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 548) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 549) if (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 550) int err;
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 551)
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 552) reiserfs_write_lock(inode->i_sb);
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 553)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 554) err = reiserfs_commit_for_inode(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 555) REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 556)
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 557) reiserfs_write_unlock(inode->i_sb);
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 558)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 559) if (err < 0)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 560) ret = err;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 561) }
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 562) out:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 563) return ret;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 564) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 565)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 566) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 567) * helper function for when reiserfs_get_block is called for a hole
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 568) * but the file tail is still in a direct item
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 569) * bh_result is the buffer head for the hole
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 570) * tail_offset is the offset of the start of the tail in the file
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 571) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 572) * This calls prepare_write, which will start a new transaction
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 573) * you should not be in a transaction, or have any paths held when you
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 574) * call this.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 575) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 576) static int convert_tail_for_hole(struct inode *inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 577) struct buffer_head *bh_result,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 578) loff_t tail_offset)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 579) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 580) unsigned long index;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 581) unsigned long tail_end;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 582) unsigned long tail_start;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 583) struct page *tail_page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 584) struct page *hole_page = bh_result->b_page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 585) int retval = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 586)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 587) if ((tail_offset & (bh_result->b_size - 1)) != 1)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 588) return -EIO;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 589)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 590) /* always try to read until the end of the block */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 591) tail_start = tail_offset & (PAGE_SIZE - 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 592) tail_end = (tail_start | (bh_result->b_size - 1)) + 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 593)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 594) index = tail_offset >> PAGE_SHIFT;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 595) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 596) * hole_page can be zero in case of direct_io, we are sure
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 597) * that we cannot get here if we write with O_DIRECT into tail page
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 598) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 599) if (!hole_page || index != hole_page->index) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 600) tail_page = grab_cache_page(inode->i_mapping, index);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 601) retval = -ENOMEM;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 602) if (!tail_page) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 603) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 604) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 605) } else {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 606) tail_page = hole_page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 607) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 608)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 609) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 610) * we don't have to make sure the conversion did not happen while
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 611) * we were locking the page because anyone that could convert
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 612) * must first take i_mutex.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 613) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 614) * We must fix the tail page for writing because it might have buffers
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 615) * that are mapped, but have a block number of 0. This indicates tail
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 616) * data that has been read directly into the page, and
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 617) * __block_write_begin won't trigger a get_block in this case.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 618) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 619) fix_tail_page_for_writing(tail_page);
ebdec241d509c (Christoph Hellwig 2010-10-06 10:47:23 +0200 620) retval = __reiserfs_write_begin(tail_page, tail_start,
ebdec241d509c (Christoph Hellwig 2010-10-06 10:47:23 +0200 621) tail_end - tail_start);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 622) if (retval)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 623) goto unlock;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 624)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 625) /* tail conversion might change the data in the page */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 626) flush_dcache_page(tail_page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 627)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 628) retval = reiserfs_commit_write(NULL, tail_page, tail_start, tail_end);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 629)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 630) unlock:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 631) if (tail_page != hole_page) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 632) unlock_page(tail_page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 633) put_page(tail_page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 634) }
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 635) out:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 636) return retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 637) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 638)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 639) static inline int _allocate_block(struct reiserfs_transaction_handle *th,
3ee1667042c35 (Jeff Mahoney 2007-10-18 23:39:25 -0700 640) sector_t block,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 641) struct inode *inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 642) b_blocknr_t * allocated_block_nr,
fec6d055da71f (Josef "Jeff" Sipek 2006-12-08 02:36:32 -0800 643) struct treepath *path, int flags)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 644) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 645) BUG_ON(!th->t_trans_id);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 646)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 647) #ifdef REISERFS_PREALLOCATE
1b1dcc1b57a49 (Jes Sorensen 2006-01-09 15:59:24 -0800 648) if (!(flags & GET_BLOCK_NO_IMUX)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 649) return reiserfs_new_unf_blocknrs2(th, inode, allocated_block_nr,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 650) path, block);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 651) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 652) #endif
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 653) return reiserfs_new_unf_blocknrs(th, inode, allocated_block_nr, path,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 654) block);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 655) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 656)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 657) int reiserfs_get_block(struct inode *inode, sector_t block,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 658) struct buffer_head *bh_result, int create)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 659) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 660) int repeat, retval = 0;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 661) /* b_blocknr_t is (unsigned) 32 bit int*/
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 662) b_blocknr_t allocated_block_nr = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 663) INITIALIZE_PATH(path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 664) int pos_in_item;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 665) struct cpu_key key;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 666) struct buffer_head *bh, *unbh = NULL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 667) struct item_head *ih, tmp_ih;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 668) __le32 *item;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 669) int done;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 670) int fs_gen;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 671) struct reiserfs_transaction_handle *th = NULL;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 672) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 673) * space reserved in transaction batch:
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 674) * . 3 balancings in direct->indirect conversion
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 675) * . 1 block involved into reiserfs_update_sd()
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 676) * XXX in practically impossible worst case direct2indirect()
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 677) * can incur (much) more than 3 balancings.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 678) * quota update for user, group
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 679) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 680) int jbegin_count =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 681) JOURNAL_PER_BALANCE_CNT * 3 + 1 +
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 682) 2 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 683) int version;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 684) int dangle = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 685) loff_t new_offset =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 686) (((loff_t) block) << inode->i_sb->s_blocksize_bits) + 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 687)
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 688) reiserfs_write_lock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 689) version = get_inode_item_key_version(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 690)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 691) if (!file_capable(inode, block)) {
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 692) reiserfs_write_unlock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 693) return -EFBIG;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 694) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 695)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 696) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 697) * if !create, we aren't changing the FS, so we don't need to
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 698) * log anything, so we don't need to start a transaction
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 699) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 700) if (!(create & GET_BLOCK_CREATE)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 701) int ret;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 702) /* find number of block-th logical block of the file */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 703) ret = _get_block_create_0(inode, block, bh_result,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 704) create | GET_BLOCK_READ_DIRECT);
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 705) reiserfs_write_unlock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 706) return ret;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 707) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 708)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 709) /*
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 710) * if we're already in a transaction, make sure to close
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 711) * any new transactions we start in this func
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 712) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 713) if ((create & GET_BLOCK_NO_DANGLE) ||
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 714) reiserfs_transaction_running(inode->i_sb))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 715) dangle = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 716)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 717) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 718) * If file is of such a size, that it might have a tail and
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 719) * tails are enabled we should mark it as possibly needing
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 720) * tail packing on close
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 721) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 722) if ((have_large_tails(inode->i_sb)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 723) && inode->i_size < i_block_size(inode) * 4)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 724) || (have_small_tails(inode->i_sb)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 725) && inode->i_size < i_block_size(inode)))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 726) REISERFS_I(inode)->i_flags |= i_pack_on_close_mask;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 727)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 728) /* set the key of the first byte in the 'block'-th block of file */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 729) make_cpu_key(&key, inode, new_offset, TYPE_ANY, 3 /*key length */ );
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 730) if ((new_offset + inode->i_sb->s_blocksize - 1) > inode->i_size) {
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 731) start_trans:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 732) th = reiserfs_persistent_transaction(inode->i_sb, jbegin_count);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 733) if (!th) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 734) retval = -ENOMEM;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 735) goto failure;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 736) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 737) reiserfs_update_inode_transaction(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 738) }
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 739) research:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 740)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 741) retval = search_for_position_by_key(inode->i_sb, &key, &path);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 742) if (retval == IO_ERROR) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 743) retval = -EIO;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 744) goto failure;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 745) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 746)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 747) bh = get_last_bh(&path);
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 748) ih = tp_item_head(&path);
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 749) item = tp_item_body(&path);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 750) pos_in_item = path.pos_in_item;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 751)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 752) fs_gen = get_generation(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 753) copy_item_head(&tmp_ih, ih);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 754)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 755) if (allocation_needed
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 756) (retval, allocated_block_nr, ih, item, pos_in_item)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 757) /* we have to allocate block for the unformatted node */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 758) if (!th) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 759) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 760) goto start_trans;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 761) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 762)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 763) repeat =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 764) _allocate_block(th, block, inode, &allocated_block_nr,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 765) &path, create);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 766)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 767) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 768) * restart the transaction to give the journal a chance to free
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 769) * some blocks. releases the path, so we have to go back to
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 770) * research if we succeed on the second try
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 771) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 772) if (repeat == NO_DISK_SPACE || repeat == QUOTA_EXCEEDED) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 773) SB_JOURNAL(inode->i_sb)->j_next_async_flush = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 774) retval = restart_transaction(th, inode, &path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 775) if (retval)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 776) goto failure;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 777) repeat =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 778) _allocate_block(th, block, inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 779) &allocated_block_nr, NULL, create);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 780)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 781) if (repeat != NO_DISK_SPACE && repeat != QUOTA_EXCEEDED) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 782) goto research;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 783) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 784) if (repeat == QUOTA_EXCEEDED)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 785) retval = -EDQUOT;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 786) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 787) retval = -ENOSPC;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 788) goto failure;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 789) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 790)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 791) if (fs_changed(fs_gen, inode->i_sb)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 792) && item_moved(&tmp_ih, &path)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 793) goto research;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 794) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 795) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 796)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 797) if (indirect_item_found(retval, ih)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 798) b_blocknr_t unfm_ptr;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 799) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 800) * 'block'-th block is in the file already (there is
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 801) * corresponding cell in some indirect item). But it may be
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 802) * zero unformatted node pointer (hole)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 803) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 804) unfm_ptr = get_block_num(item, pos_in_item);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 805) if (unfm_ptr == 0) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 806) /* use allocated block to plug the hole */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 807) reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 808) if (fs_changed(fs_gen, inode->i_sb)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 809) && item_moved(&tmp_ih, &path)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 810) reiserfs_restore_prepared_buffer(inode->i_sb,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 811) bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 812) goto research;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 813) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 814) set_buffer_new(bh_result);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 815) if (buffer_dirty(bh_result)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 816) && reiserfs_data_ordered(inode->i_sb))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 817) reiserfs_add_ordered_list(inode, bh_result);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 818) put_block_num(item, pos_in_item, allocated_block_nr);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 819) unfm_ptr = allocated_block_nr;
09f1b80ba8c96 (Jeff Mahoney 2014-04-23 10:00:39 -0400 820) journal_mark_dirty(th, bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 821) reiserfs_update_sd(th, inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 822) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 823) set_block_dev_mapped(bh_result, unfm_ptr, inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 824) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 825) retval = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 826) if (!dangle && th)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 827) retval = reiserfs_end_persistent_transaction(th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 828)
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 829) reiserfs_write_unlock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 830)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 831) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 832) * the item was found, so new blocks were not added to the file
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 833) * there is no need to make sure the inode is updated with this
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 834) * transaction
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 835) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 836) return retval;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 837) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 838)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 839) if (!th) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 840) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 841) goto start_trans;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 842) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 843)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 844) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 845) * desired position is not found or is in the direct item. We have
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 846) * to append file with holes up to 'block'-th block converting
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 847) * direct items to indirect one if necessary
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 848) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 849) done = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 850) do {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 851) if (is_statdata_le_ih(ih)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 852) __le32 unp = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 853) struct cpu_key tmp_key;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 854)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 855) /* indirect item has to be inserted */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 856) make_le_item_head(&tmp_ih, &key, version, 1,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 857) TYPE_INDIRECT, UNFM_P_SIZE,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 858) 0 /* free_space */ );
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 859)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 860) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 861) * we are going to add 'block'-th block to the file.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 862) * Use allocated block for that
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 863) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 864) if (cpu_key_k_offset(&key) == 1) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 865) unp = cpu_to_le32(allocated_block_nr);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 866) set_block_dev_mapped(bh_result,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 867) allocated_block_nr, inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 868) set_buffer_new(bh_result);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 869) done = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 870) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 871) tmp_key = key; /* ;) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 872) set_cpu_key_k_offset(&tmp_key, 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 873) PATH_LAST_POSITION(&path)++;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 874)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 875) retval =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 876) reiserfs_insert_item(th, &path, &tmp_key, &tmp_ih,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 877) inode, (char *)&unp);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 878) if (retval) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 879) reiserfs_free_block(th, inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 880) allocated_block_nr, 1);
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 881) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 882) * retval == -ENOSPC, -EDQUOT or -EIO
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 883) * or -EEXIST
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 884) */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 885) goto failure;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 886) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 887) } else if (is_direct_le_ih(ih)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 888) /* direct item has to be converted */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 889) loff_t tail_offset;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 890)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 891) tail_offset =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 892) ((le_ih_k_offset(ih) -
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 893) 1) & ~(inode->i_sb->s_blocksize - 1)) + 1;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 894)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 895) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 896) * direct item we just found fits into block we have
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 897) * to map. Convert it into unformatted node: use
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 898) * bh_result for the conversion
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 899) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 900) if (tail_offset == cpu_key_k_offset(&key)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 901) set_block_dev_mapped(bh_result,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 902) allocated_block_nr, inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 903) unbh = bh_result;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 904) done = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 905) } else {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 906) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 907) * we have to pad file tail stored in direct
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 908) * item(s) up to block size and convert it
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 909) * to unformatted node. FIXME: this should
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 910) * also get into page cache
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 911) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 912)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 913) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 914) /*
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 915) * ugly, but we can only end the transaction if
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 916) * we aren't nested
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 917) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 918) BUG_ON(!th->t_refcount);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 919) if (th->t_refcount == 1) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 920) retval =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 921) reiserfs_end_persistent_transaction
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 922) (th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 923) th = NULL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 924) if (retval)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 925) goto failure;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 926) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 927)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 928) retval =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 929) convert_tail_for_hole(inode, bh_result,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 930) tail_offset);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 931) if (retval) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 932) if (retval != -ENOSPC)
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 933) reiserfs_error(inode->i_sb,
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 934) "clm-6004",
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 935) "convert tail failed "
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 936) "inode %lu, error %d",
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 937) inode->i_ino,
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 938) retval);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 939) if (allocated_block_nr) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 940) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 941) * the bitmap, the super,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 942) * and the stat data == 3
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 943) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 944) if (!th)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 945) th = reiserfs_persistent_transaction(inode->i_sb, 3);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 946) if (th)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 947) reiserfs_free_block(th,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 948) inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 949) allocated_block_nr,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 950) 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 951) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 952) goto failure;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 953) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 954) goto research;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 955) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 956) retval =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 957) direct2indirect(th, inode, &path, unbh,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 958) tail_offset);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 959) if (retval) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 960) reiserfs_unmap_buffer(unbh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 961) reiserfs_free_block(th, inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 962) allocated_block_nr, 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 963) goto failure;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 964) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 965) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 966) * it is important the set_buffer_uptodate is done
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 967) * after the direct2indirect. The buffer might
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 968) * contain valid data newer than the data on disk
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 969) * (read by readpage, changed, and then sent here by
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 970) * writepage). direct2indirect needs to know if unbh
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 971) * was already up to date, so it can decide if the
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 972) * data in unbh needs to be replaced with data from
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 973) * the disk
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 974) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 975) set_buffer_uptodate(unbh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 976)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 977) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 978) * unbh->b_page == NULL in case of DIRECT_IO request,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 979) * this means buffer will disappear shortly, so it
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 980) * should not be added to
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 981) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 982) if (unbh->b_page) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 983) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 984) * we've converted the tail, so we must
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 985) * flush unbh before the transaction commits
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 986) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 987) reiserfs_add_tail_list(inode, unbh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 988)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 989) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 990) * mark it dirty now to prevent commit_write
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 991) * from adding this buffer to the inode's
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 992) * dirty buffer list
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 993) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 994) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 995) * AKPM: changed __mark_buffer_dirty to
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 996) * mark_buffer_dirty(). It's still atomic,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 997) * but it sets the page dirty too, which makes
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 998) * it eligible for writeback at any time by the
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 999) * VM (which was also the case with
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1000) * __mark_buffer_dirty())
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1001) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1002) mark_buffer_dirty(unbh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1003) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1004) } else {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1005) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1006) * append indirect item with holes if needed, when
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1007) * appending pointer to 'block'-th block use block,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1008) * which is already allocated
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1009) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1010) struct cpu_key tmp_key;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1011) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1012) * We use this in case we need to allocate
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1013) * only one block which is a fastpath
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1014) */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1015) unp_t unf_single = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1016) unp_t *un;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1017) __u64 max_to_insert =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1018) MAX_ITEM_LEN(inode->i_sb->s_blocksize) /
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1019) UNFM_P_SIZE;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1020) __u64 blocks_needed;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1021)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1022) RFALSE(pos_in_item != ih_item_len(ih) / UNFM_P_SIZE,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1023) "vs-804: invalid position for append");
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1024) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1025) * indirect item has to be appended,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1026) * set up key of that position
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1027) * (key type is unimportant)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1028) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1029) make_cpu_key(&tmp_key, inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1030) le_key_k_offset(version,
a228bf8f0a3e5 (Jeff Mahoney 2014-04-23 10:00:42 -0400 1031) &ih->ih_key) +
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1032) op_bytes_number(ih,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1033) inode->i_sb->s_blocksize),
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1034) TYPE_INDIRECT, 3);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1035)
c499ec24c31ed (Vladimir V. Saveliev 2006-03-02 02:54:39 -0800 1036) RFALSE(cpu_key_k_offset(&tmp_key) > cpu_key_k_offset(&key),
c499ec24c31ed (Vladimir V. Saveliev 2006-03-02 02:54:39 -0800 1037) "green-805: invalid offset");
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1038) blocks_needed =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1039) 1 +
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1040) ((cpu_key_k_offset(&key) -
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1041) cpu_key_k_offset(&tmp_key)) >> inode->i_sb->
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1042) s_blocksize_bits);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1043)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1044) if (blocks_needed == 1) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1045) un = &unf_single;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1046) } else {
6396bb221514d (Kees Cook 2018-06-12 14:03:40 -0700 1047) un = kcalloc(min(blocks_needed, max_to_insert),
6396bb221514d (Kees Cook 2018-06-12 14:03:40 -0700 1048) UNFM_P_SIZE, GFP_NOFS);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1049) if (!un) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1050) un = &unf_single;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1051) blocks_needed = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1052) max_to_insert = 0;
01afb2134ed07 (Yan Burman 2006-12-06 20:39:01 -0800 1053) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1054) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1055) if (blocks_needed <= max_to_insert) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1056) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1057) * we are going to add target block to
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1058) * the file. Use allocated block for that
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1059) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1060) un[blocks_needed - 1] =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1061) cpu_to_le32(allocated_block_nr);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1062) set_block_dev_mapped(bh_result,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1063) allocated_block_nr, inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1064) set_buffer_new(bh_result);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1065) done = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1066) } else {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1067) /* paste hole to the indirect item */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1068) /*
5626de1e96f75 (Liao Pingfang 2020-05-29 09:02:30 +0800 1069) * If kcalloc failed, max_to_insert becomes
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1070) * zero and it means we only have space for
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1071) * one block
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1072) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1073) blocks_needed =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1074) max_to_insert ? max_to_insert : 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1075) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1076) retval =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1077) reiserfs_paste_into_item(th, &path, &tmp_key, inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1078) (char *)un,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1079) UNFM_P_SIZE *
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1080) blocks_needed);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1081)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1082) if (blocks_needed != 1)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1083) kfree(un);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1084)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1085) if (retval) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1086) reiserfs_free_block(th, inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1087) allocated_block_nr, 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1088) goto failure;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1089) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1090) if (!done) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1091) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1092) * We need to mark new file size in case
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1093) * this function will be interrupted/aborted
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1094) * later on. And we may do this only for
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1095) * holes.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1096) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1097) inode->i_size +=
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1098) inode->i_sb->s_blocksize * blocks_needed;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1099) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1100) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1101)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1102) if (done == 1)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1103) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1104)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1105) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1106) * this loop could log more blocks than we had originally
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1107) * asked for. So, we have to allow the transaction to end
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1108) * if it is too big or too full. Update the inode so things
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1109) * are consistent if we crash before the function returns
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1110) * release the path so that anybody waiting on the path before
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1111) * ending their transaction will be able to continue.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1112) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1113) if (journal_transaction_should_end(th, th->t_blocks_allocated)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1114) retval = restart_transaction(th, inode, &path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1115) if (retval)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1116) goto failure;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1117) }
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 1118) /*
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 1119) * inserting indirect pointers for a hole can take a
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 1120) * long time. reschedule if needed and also release the write
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 1121) * lock for others.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1122) */
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 1123) reiserfs_cond_resched(inode->i_sb);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1124)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1125) retval = search_for_position_by_key(inode->i_sb, &key, &path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1126) if (retval == IO_ERROR) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1127) retval = -EIO;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1128) goto failure;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1129) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1130) if (retval == POSITION_FOUND) {
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 1131) reiserfs_warning(inode->i_sb, "vs-825",
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1132) "%K should not be found", &key);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1133) retval = -EEXIST;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1134) if (allocated_block_nr)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1135) reiserfs_free_block(th, inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1136) allocated_block_nr, 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1137) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1138) goto failure;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1139) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1140) bh = get_last_bh(&path);
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 1141) ih = tp_item_head(&path);
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 1142) item = tp_item_body(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1143) pos_in_item = path.pos_in_item;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1144) } while (1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1145)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1146) retval = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1147)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 1148) failure:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1149) if (th && (!dangle || (retval && !th->t_trans_id))) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1150) int err;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1151) if (th->t_trans_id)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1152) reiserfs_update_sd(th, inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1153) err = reiserfs_end_persistent_transaction(th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1154) if (err)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1155) retval = err;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1156) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1157)
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 1158) reiserfs_write_unlock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1159) reiserfs_check_path(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1160) return retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1161) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1162)
d4388340ae0bc (Matthew Wilcox (Oracle) 2020-06-01 21:47:02 -0700 1163) static void reiserfs_readahead(struct readahead_control *rac)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1164) {
d4388340ae0bc (Matthew Wilcox (Oracle) 2020-06-01 21:47:02 -0700 1165) mpage_readahead(rac, reiserfs_get_block);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1166) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1167)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1168) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1169) * Compute real number of used bytes by file
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1170) * Following three functions can go away when we'll have enough space in
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1171) * stat item
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1172) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1173) static int real_space_diff(struct inode *inode, int sd_size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1174) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1175) int bytes;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1176) loff_t blocksize = inode->i_sb->s_blocksize;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1177)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1178) if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1179) return sd_size;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1180)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1181) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1182) * End of file is also in full block with indirect reference, so round
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1183) * up to the next block.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1184) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1185) * there is just no way to know if the tail is actually packed
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1186) * on the file, so we have to assume it isn't. When we pack the
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1187) * tail, we add 4 bytes to pretend there really is an unformatted
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1188) * node pointer
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1189) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1190) bytes =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1191) ((inode->i_size +
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1192) (blocksize - 1)) >> inode->i_sb->s_blocksize_bits) * UNFM_P_SIZE +
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1193) sd_size;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1194) return bytes;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1195) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1196)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1197) static inline loff_t to_real_used_space(struct inode *inode, ulong blocks,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1198) int sd_size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1199) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1200) if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1201) return inode->i_size +
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1202) (loff_t) (real_space_diff(inode, sd_size));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1203) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1204) return ((loff_t) real_space_diff(inode, sd_size)) +
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1205) (((loff_t) blocks) << 9);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1206) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1207)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1208) /* Compute number of blocks used by file in ReiserFS counting */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1209) static inline ulong to_fake_used_blocks(struct inode *inode, int sd_size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1210) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1211) loff_t bytes = inode_get_bytes(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1212) loff_t real_space = real_space_diff(inode, sd_size);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1213)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1214) /* keeps fsck and non-quota versions of reiserfs happy */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1215) if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1216) bytes += (loff_t) 511;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1217) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1218)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1219) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1220) * files from before the quota patch might i_blocks such that
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1221) * bytes < real_space. Deal with that here to prevent it from
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1222) * going negative.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1223) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1224) if (bytes < real_space)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1225) return 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1226) return (bytes - real_space) >> 9;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1227) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1228)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1229) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1230) * BAD: new directories have stat data of new type and all other items
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1231) * of old type. Version stored in the inode says about body items, so
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1232) * in update_stat_data we can not rely on inode, but have to check
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1233) * item version directly
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1234) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1235)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1236) /* called by read_locked_inode */
fec6d055da71f (Josef "Jeff" Sipek 2006-12-08 02:36:32 -0800 1237) static void init_inode(struct inode *inode, struct treepath *path)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1238) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1239) struct buffer_head *bh;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1240) struct item_head *ih;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1241) __u32 rdev;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1242)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1243) bh = PATH_PLAST_BUFFER(path);
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 1244) ih = tp_item_head(path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1245)
a228bf8f0a3e5 (Jeff Mahoney 2014-04-23 10:00:42 -0400 1246) copy_key(INODE_PKEY(inode), &ih->ih_key);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1247)
a228bf8f0a3e5 (Jeff Mahoney 2014-04-23 10:00:42 -0400 1248) INIT_LIST_HEAD(&REISERFS_I(inode)->i_prealloc_list);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1249) REISERFS_I(inode)->i_flags = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1250) REISERFS_I(inode)->i_prealloc_block = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1251) REISERFS_I(inode)->i_prealloc_count = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1252) REISERFS_I(inode)->i_trans_id = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1253) REISERFS_I(inode)->i_jl = NULL;
068fbb315dd1e (Alexey Dobriyan 2006-09-29 01:59:58 -0700 1254) reiserfs_init_xattr_rwsem(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1255)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1256) if (stat_data_v1(ih)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1257) struct stat_data_v1 *sd =
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 1258) (struct stat_data_v1 *)ih_item_body(bh, ih);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1259) unsigned long blocks;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1260)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1261) set_inode_item_key_version(inode, KEY_FORMAT_3_5);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1262) set_inode_sd_version(inode, STAT_DATA_V1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1263) inode->i_mode = sd_v1_mode(sd);
bfe8684869601 (Miklos Szeredi 2011-10-28 14:13:29 +0200 1264) set_nlink(inode, sd_v1_nlink(sd));
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 1265) i_uid_write(inode, sd_v1_uid(sd));
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 1266) i_gid_write(inode, sd_v1_gid(sd));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1267) inode->i_size = sd_v1_size(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1268) inode->i_atime.tv_sec = sd_v1_atime(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1269) inode->i_mtime.tv_sec = sd_v1_mtime(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1270) inode->i_ctime.tv_sec = sd_v1_ctime(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1271) inode->i_atime.tv_nsec = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1272) inode->i_ctime.tv_nsec = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1273) inode->i_mtime.tv_nsec = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1274)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1275) inode->i_blocks = sd_v1_blocks(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1276) inode->i_generation = le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1277) blocks = (inode->i_size + 511) >> 9;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1278) blocks = _ROUND_UP(blocks, inode->i_sb->s_blocksize >> 9);
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1279)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1280) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1281) * there was a bug in <=3.5.23 when i_blocks could take
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1282) * negative values. Starting from 3.5.17 this value could
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1283) * even be stored in stat data. For such files we set
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1284) * i_blocks based on file size. Just 2 notes: this can be
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1285) * wrong for sparse files. On-disk value will be only
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1286) * updated if file's inode will ever change
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1287) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1288) if (inode->i_blocks > blocks) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1289) inode->i_blocks = blocks;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1290) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1291)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1292) rdev = sd_v1_rdev(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1293) REISERFS_I(inode)->i_first_direct_byte =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1294) sd_v1_first_direct_byte(sd);
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1295)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1296) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1297) * an early bug in the quota code can give us an odd
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1298) * number for the block count. This is incorrect, fix it here.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1299) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1300) if (inode->i_blocks & 1) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1301) inode->i_blocks++;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1302) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1303) inode_set_bytes(inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1304) to_real_used_space(inode, inode->i_blocks,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1305) SD_V1_SIZE));
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1306) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1307) * nopack is initially zero for v1 objects. For v2 objects,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1308) * nopack is initialised from sd_attrs
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1309) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1310) REISERFS_I(inode)->i_flags &= ~i_nopack_mask;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1311) } else {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1312) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1313) * new stat data found, but object may have old items
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1314) * (directories and symlinks)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1315) */
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 1316) struct stat_data *sd = (struct stat_data *)ih_item_body(bh, ih);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1317)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1318) inode->i_mode = sd_v2_mode(sd);
bfe8684869601 (Miklos Szeredi 2011-10-28 14:13:29 +0200 1319) set_nlink(inode, sd_v2_nlink(sd));
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 1320) i_uid_write(inode, sd_v2_uid(sd));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1321) inode->i_size = sd_v2_size(sd);
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 1322) i_gid_write(inode, sd_v2_gid(sd));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1323) inode->i_mtime.tv_sec = sd_v2_mtime(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1324) inode->i_atime.tv_sec = sd_v2_atime(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1325) inode->i_ctime.tv_sec = sd_v2_ctime(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1326) inode->i_ctime.tv_nsec = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1327) inode->i_mtime.tv_nsec = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1328) inode->i_atime.tv_nsec = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1329) inode->i_blocks = sd_v2_blocks(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1330) rdev = sd_v2_rdev(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1331) if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1332) inode->i_generation =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1333) le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1334) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1335) inode->i_generation = sd_v2_generation(sd);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1336)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1337) if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1338) set_inode_item_key_version(inode, KEY_FORMAT_3_5);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1339) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1340) set_inode_item_key_version(inode, KEY_FORMAT_3_6);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1341) REISERFS_I(inode)->i_first_direct_byte = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1342) set_inode_sd_version(inode, STAT_DATA_V2);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1343) inode_set_bytes(inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1344) to_real_used_space(inode, inode->i_blocks,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1345) SD_V2_SIZE));
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1346) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1347) * read persistent inode attributes from sd and initialise
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1348) * generic inode flags from them
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1349) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1350) REISERFS_I(inode)->i_attrs = sd_v2_attrs(sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1351) sd_attrs_to_i_attrs(sd_v2_attrs(sd), inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1352) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1353)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1354) pathrelse(path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1355) if (S_ISREG(inode->i_mode)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1356) inode->i_op = &reiserfs_file_inode_operations;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1357) inode->i_fop = &reiserfs_file_operations;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1358) inode->i_mapping->a_ops = &reiserfs_address_space_operations;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1359) } else if (S_ISDIR(inode->i_mode)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1360) inode->i_op = &reiserfs_dir_inode_operations;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1361) inode->i_fop = &reiserfs_dir_operations;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1362) } else if (S_ISLNK(inode->i_mode)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1363) inode->i_op = &reiserfs_symlink_inode_operations;
21fc61c73c390 (Al Viro 2015-11-17 01:07:57 -0500 1364) inode_nohighmem(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1365) inode->i_mapping->a_ops = &reiserfs_address_space_operations;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1366) } else {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1367) inode->i_blocks = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1368) inode->i_op = &reiserfs_special_inode_operations;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1369) init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1370) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1371) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1372)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1373) /* update new stat data with inode fields */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1374) static void inode2sd(void *sd, struct inode *inode, loff_t size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1375) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1376) struct stat_data *sd_v2 = (struct stat_data *)sd;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1377)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1378) set_sd_v2_mode(sd_v2, inode->i_mode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1379) set_sd_v2_nlink(sd_v2, inode->i_nlink);
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 1380) set_sd_v2_uid(sd_v2, i_uid_read(inode));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1381) set_sd_v2_size(sd_v2, size);
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 1382) set_sd_v2_gid(sd_v2, i_gid_read(inode));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1383) set_sd_v2_mtime(sd_v2, inode->i_mtime.tv_sec);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1384) set_sd_v2_atime(sd_v2, inode->i_atime.tv_sec);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1385) set_sd_v2_ctime(sd_v2, inode->i_ctime.tv_sec);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1386) set_sd_v2_blocks(sd_v2, to_fake_used_blocks(inode, SD_V2_SIZE));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1387) if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1388) set_sd_v2_rdev(sd_v2, new_encode_dev(inode->i_rdev));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1389) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1390) set_sd_v2_generation(sd_v2, inode->i_generation);
a480b5bebdeac (Jan Kara 2017-04-11 16:51:34 +0200 1391) set_sd_v2_attrs(sd_v2, REISERFS_I(inode)->i_attrs);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1392) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1393)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1394) /* used to copy inode's fields to old stat data */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1395) static void inode2sd_v1(void *sd, struct inode *inode, loff_t size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1396) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1397) struct stat_data_v1 *sd_v1 = (struct stat_data_v1 *)sd;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1398)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1399) set_sd_v1_mode(sd_v1, inode->i_mode);
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 1400) set_sd_v1_uid(sd_v1, i_uid_read(inode));
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 1401) set_sd_v1_gid(sd_v1, i_gid_read(inode));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1402) set_sd_v1_nlink(sd_v1, inode->i_nlink);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1403) set_sd_v1_size(sd_v1, size);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1404) set_sd_v1_atime(sd_v1, inode->i_atime.tv_sec);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1405) set_sd_v1_ctime(sd_v1, inode->i_ctime.tv_sec);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1406) set_sd_v1_mtime(sd_v1, inode->i_mtime.tv_sec);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1407)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1408) if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1409) set_sd_v1_rdev(sd_v1, new_encode_dev(inode->i_rdev));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1410) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1411) set_sd_v1_blocks(sd_v1, to_fake_used_blocks(inode, SD_V1_SIZE));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1412)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1413) /* Sigh. i_first_direct_byte is back */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1414) set_sd_v1_first_direct_byte(sd_v1,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1415) REISERFS_I(inode)->i_first_direct_byte);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1416) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1417)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1418) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1419) * NOTE, you must prepare the buffer head before sending it here,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1420) * and then log it after the call
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1421) */
fec6d055da71f (Josef "Jeff" Sipek 2006-12-08 02:36:32 -0800 1422) static void update_stat_data(struct treepath *path, struct inode *inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1423) loff_t size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1424) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1425) struct buffer_head *bh;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1426) struct item_head *ih;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1427)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1428) bh = PATH_PLAST_BUFFER(path);
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 1429) ih = tp_item_head(path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1430)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1431) if (!is_statdata_le_ih(ih))
c3a9c2109f848 (Jeff Mahoney 2009-03-30 14:02:25 -0400 1432) reiserfs_panic(inode->i_sb, "vs-13065", "key %k, found item %h",
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1433) INODE_PKEY(inode), ih);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1434)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1435) /* path points to old stat data */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1436) if (stat_data_v1(ih)) {
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 1437) inode2sd_v1(ih_item_body(bh, ih), inode, size);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1438) } else {
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 1439) inode2sd(ih_item_body(bh, ih), inode, size);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1440) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1441)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1442) return;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1443) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1444)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1445) void reiserfs_update_sd_size(struct reiserfs_transaction_handle *th,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1446) struct inode *inode, loff_t size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1447) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1448) struct cpu_key key;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1449) INITIALIZE_PATH(path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1450) struct buffer_head *bh;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1451) int fs_gen;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1452) struct item_head *ih, tmp_ih;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1453) int retval;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1454)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1455) BUG_ON(!th->t_trans_id);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1456)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1457) /* key type is unimportant */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1458) make_cpu_key(&key, inode, SD_OFFSET, TYPE_STAT_DATA, 3);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1459)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1460) for (;;) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1461) int pos;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1462) /* look for the object's stat data */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1463) retval = search_item(inode->i_sb, &key, &path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1464) if (retval == IO_ERROR) {
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 1465) reiserfs_error(inode->i_sb, "vs-13050",
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 1466) "i/o failure occurred trying to "
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 1467) "update %K stat data", &key);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1468) return;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1469) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1470) if (retval == ITEM_NOT_FOUND) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1471) pos = PATH_LAST_POSITION(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1472) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1473) if (inode->i_nlink == 0) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1474) /*reiserfs_warning (inode->i_sb, "vs-13050: reiserfs_update_sd: i_nlink == 0, stat data not found"); */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1475) return;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1476) }
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 1477) reiserfs_warning(inode->i_sb, "vs-13060",
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 1478) "stat data of object %k (nlink == %d) "
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 1479) "not found (pos %d)",
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1480) INODE_PKEY(inode), inode->i_nlink,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1481) pos);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1482) reiserfs_check_path(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1483) return;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1484) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1485)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1486) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1487) * sigh, prepare_for_journal might schedule. When it
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1488) * schedules the FS might change. We have to detect that,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1489) * and loop back to the search if the stat data item has moved
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1490) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1491) bh = get_last_bh(&path);
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 1492) ih = tp_item_head(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1493) copy_item_head(&tmp_ih, ih);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1494) fs_gen = get_generation(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1495) reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1496)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1497) /* Stat_data item has been moved after scheduling. */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1498) if (fs_changed(fs_gen, inode->i_sb)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1499) && item_moved(&tmp_ih, &path)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1500) reiserfs_restore_prepared_buffer(inode->i_sb, bh);
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1501) continue;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1502) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1503) break;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1504) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1505) update_stat_data(&path, inode, size);
09f1b80ba8c96 (Jeff Mahoney 2014-04-23 10:00:39 -0400 1506) journal_mark_dirty(th, bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1507) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1508) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1509) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1510)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1511) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1512) * reiserfs_read_locked_inode is called to read the inode off disk, and it
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1513) * does a make_bad_inode when things go wrong. But, we need to make sure
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1514) * and clear the key in the private portion of the inode, otherwise a
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1515) * corresponding iput might try to delete whatever object the inode last
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1516) * represented.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1517) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1518) static void reiserfs_make_bad_inode(struct inode *inode)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1519) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1520) memset(INODE_PKEY(inode), 0, KEY_SIZE);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1521) make_bad_inode(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1522) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1523)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1524) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1525) * initially this function was derived from minix or ext2's analog and
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1526) * evolved as the prototype did
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1527) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1528) int reiserfs_init_locked_inode(struct inode *inode, void *p)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1529) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1530) struct reiserfs_iget_args *args = (struct reiserfs_iget_args *)p;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1531) inode->i_ino = args->objectid;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1532) INODE_PKEY(inode)->k_dir_id = cpu_to_le32(args->dirid);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1533) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1534) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1535)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1536) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1537) * looks for stat data in the tree, and fills up the fields of in-core
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1538) * inode stat data fields
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1539) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1540) void reiserfs_read_locked_inode(struct inode *inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1541) struct reiserfs_iget_args *args)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1542) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1543) INITIALIZE_PATH(path_to_sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1544) struct cpu_key key;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1545) unsigned long dirino;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1546) int retval;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1547)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1548) dirino = args->dirid;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1549)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1550) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1551) * set version 1, version 2 could be used too, because stat data
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1552) * key is the same in both versions
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1553) */
4443390e08d34 (Jan Kara 2020-09-21 15:08:50 +0200 1554) _make_cpu_key(&key, KEY_FORMAT_3_5, dirino, inode->i_ino, 0, 0, 3);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1555)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1556) /* look for the object's stat data */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1557) retval = search_item(inode->i_sb, &key, &path_to_sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1558) if (retval == IO_ERROR) {
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 1559) reiserfs_error(inode->i_sb, "vs-13070",
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 1560) "i/o failure occurred trying to find "
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 1561) "stat data of %K", &key);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1562) reiserfs_make_bad_inode(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1563) return;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1564) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1565)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1566) /* a stale NFS handle can trigger this without it being an error */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1567) if (retval != ITEM_FOUND) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1568) pathrelse(&path_to_sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1569) reiserfs_make_bad_inode(inode);
6d6b77f163c7e (Miklos Szeredi 2011-10-28 14:13:28 +0200 1570) clear_nlink(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1571) return;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1572) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1573)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1574) init_inode(inode, &path_to_sd);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1575)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1576) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1577) * It is possible that knfsd is trying to access inode of a file
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1578) * that is being removed from the disk by some other thread. As we
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1579) * update sd on unlink all that is required is to check for nlink
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1580) * here. This bug was first found by Sizif when debugging
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1581) * SquidNG/Butterfly, forgotten, and found again after Philippe
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1582) * Gramoulle <philippe.gramoulle@mmania.com> reproduced it.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1583)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1584) * More logical fix would require changes in fs/inode.c:iput() to
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1585) * remove inode from hash-table _after_ fs cleaned disk stuff up and
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1586) * in iget() to return NULL if I_FREEING inode is found in
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1587) * hash-table.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1588) */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1589)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1590) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1591) * Currently there is one place where it's ok to meet inode with
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1592) * nlink==0: processing of open-unlinked and half-truncated files
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1593) * during mount (fs/reiserfs/super.c:finish_unfinished()).
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1594) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1595) if ((inode->i_nlink == 0) &&
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1596) !REISERFS_SB(inode->i_sb)->s_is_unlinked_ok) {
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 1597) reiserfs_warning(inode->i_sb, "vs-13075",
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1598) "dead inode read from disk %K. "
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1599) "This is likely to be race with knfsd. Ignore",
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1600) &key);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1601) reiserfs_make_bad_inode(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1602) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1603)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1604) /* init inode should be relsing */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1605) reiserfs_check_path(&path_to_sd);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1606)
4482a087d4c5a (Christoph Hellwig 2011-07-23 17:37:03 +0200 1607) /*
4482a087d4c5a (Christoph Hellwig 2011-07-23 17:37:03 +0200 1608) * Stat data v1 doesn't support ACLs.
4482a087d4c5a (Christoph Hellwig 2011-07-23 17:37:03 +0200 1609) */
4482a087d4c5a (Christoph Hellwig 2011-07-23 17:37:03 +0200 1610) if (get_inode_sd_version(inode) == STAT_DATA_V1)
4482a087d4c5a (Christoph Hellwig 2011-07-23 17:37:03 +0200 1611) cache_no_acl(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1612) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1613)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1614) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1615) * reiserfs_find_actor() - "find actor" reiserfs supplies to iget5_locked().
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1616) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1617) * @inode: inode from hash table to check
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1618) * @opaque: "cookie" passed to iget5_locked(). This is &reiserfs_iget_args.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1619) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1620) * This function is called by iget5_locked() to distinguish reiserfs inodes
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1621) * having the same inode numbers. Such inodes can only exist due to some
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1622) * error condition. One of them should be bad. Inodes with identical
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1623) * inode numbers (objectids) are distinguished by parent directory ids.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1624) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1625) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1626) int reiserfs_find_actor(struct inode *inode, void *opaque)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1627) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1628) struct reiserfs_iget_args *args;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1629)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1630) args = opaque;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1631) /* args is already in CPU order */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1632) return (inode->i_ino == args->objectid) &&
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1633) (le32_to_cpu(INODE_PKEY(inode)->k_dir_id) == args->dirid);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1634) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1635)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1636) struct inode *reiserfs_iget(struct super_block *s, const struct cpu_key *key)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1637) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1638) struct inode *inode;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1639) struct reiserfs_iget_args args;
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 1640) int depth;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1641)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1642) args.objectid = key->on_disk_key.k_objectid;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1643) args.dirid = key->on_disk_key.k_dir_id;
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 1644) depth = reiserfs_write_unlock_nested(s);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1645) inode = iget5_locked(s, key->on_disk_key.k_objectid,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1646) reiserfs_find_actor, reiserfs_init_locked_inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1647) (void *)(&args));
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 1648) reiserfs_write_lock_nested(s, depth);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1649) if (!inode)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1650) return ERR_PTR(-ENOMEM);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1651)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1652) if (inode->i_state & I_NEW) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1653) reiserfs_read_locked_inode(inode, &args);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1654) unlock_new_inode(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1655) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1656)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1657) if (comp_short_keys(INODE_PKEY(inode), key) || is_bad_inode(inode)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1658) /* either due to i/o error or a stale NFS handle */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1659) iput(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1660) inode = NULL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1661) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1662) return inode;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1663) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1664)
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1665) static struct dentry *reiserfs_get_dentry(struct super_block *sb,
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1666) u32 objectid, u32 dir_id, u32 generation)
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1667)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1668) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1669) struct cpu_key key;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1670) struct inode *inode;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1671)
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1672) key.on_disk_key.k_objectid = objectid;
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1673) key.on_disk_key.k_dir_id = dir_id;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1674) reiserfs_write_lock(sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1675) inode = reiserfs_iget(sb, &key);
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1676) if (inode && !IS_ERR(inode) && generation != 0 &&
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1677) generation != inode->i_generation) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1678) iput(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1679) inode = NULL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1680) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1681) reiserfs_write_unlock(sb);
440037287c5eb (Christoph Hellwig 2008-08-11 15:49:04 +0200 1682)
440037287c5eb (Christoph Hellwig 2008-08-11 15:49:04 +0200 1683) return d_obtain_alias(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1684) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1685)
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1686) struct dentry *reiserfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1687) int fh_len, int fh_type)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1688) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1689) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1690) * fhtype happens to reflect the number of u32s encoded.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1691) * due to a bug in earlier code, fhtype might indicate there
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1692) * are more u32s then actually fitted.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1693) * so if fhtype seems to be more than len, reduce fhtype.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1694) * Valid types are:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1695) * 2 - objectid + dir_id - legacy support
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1696) * 3 - objectid + dir_id + generation
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1697) * 4 - objectid + dir_id + objectid and dirid of parent - legacy
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1698) * 5 - objectid + dir_id + generation + objectid and dirid of parent
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1699) * 6 - as above plus generation of directory
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1700) * 6 does not fit in NFSv2 handles
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1701) */
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1702) if (fh_type > fh_len) {
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1703) if (fh_type != 6 || fh_len != 5)
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 1704) reiserfs_warning(sb, "reiserfs-13077",
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1705) "nfsd/reiserfs, fhtype=%d, len=%d - odd",
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1706) fh_type, fh_len);
35c2a7f4908d4 (Hugh Dickins 2012-10-07 20:32:51 -0700 1707) fh_type = fh_len;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1708) }
35c2a7f4908d4 (Hugh Dickins 2012-10-07 20:32:51 -0700 1709) if (fh_len < 2)
35c2a7f4908d4 (Hugh Dickins 2012-10-07 20:32:51 -0700 1710) return NULL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1711)
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1712) return reiserfs_get_dentry(sb, fid->raw[0], fid->raw[1],
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1713) (fh_type == 3 || fh_type >= 5) ? fid->raw[2] : 0);
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1714) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1715)
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1716) struct dentry *reiserfs_fh_to_parent(struct super_block *sb, struct fid *fid,
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1717) int fh_len, int fh_type)
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1718) {
35c2a7f4908d4 (Hugh Dickins 2012-10-07 20:32:51 -0700 1719) if (fh_type > fh_len)
35c2a7f4908d4 (Hugh Dickins 2012-10-07 20:32:51 -0700 1720) fh_type = fh_len;
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1721) if (fh_type < 4)
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1722) return NULL;
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1723)
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1724) return reiserfs_get_dentry(sb,
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1725) (fh_type >= 5) ? fid->raw[3] : fid->raw[2],
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1726) (fh_type >= 5) ? fid->raw[4] : fid->raw[3],
be55caf177e14 (Christoph Hellwig 2007-10-21 16:42:13 -0700 1727) (fh_type == 6) ? fid->raw[5] : 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1728) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1729)
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 1730) int reiserfs_encode_fh(struct inode *inode, __u32 * data, int *lenp,
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 1731) struct inode *parent)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1732) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1733) int maxlen = *lenp;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1734)
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 1735) if (parent && (maxlen < 5)) {
5fe0c2378884e (Aneesh Kumar K.V 2011-01-29 18:43:25 +0530 1736) *lenp = 5;
94e07a7590ae8 (Namjae Jeon 2013-02-17 15:48:11 +0900 1737) return FILEID_INVALID;
5fe0c2378884e (Aneesh Kumar K.V 2011-01-29 18:43:25 +0530 1738) } else if (maxlen < 3) {
5fe0c2378884e (Aneesh Kumar K.V 2011-01-29 18:43:25 +0530 1739) *lenp = 3;
94e07a7590ae8 (Namjae Jeon 2013-02-17 15:48:11 +0900 1740) return FILEID_INVALID;
5fe0c2378884e (Aneesh Kumar K.V 2011-01-29 18:43:25 +0530 1741) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1742)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1743) data[0] = inode->i_ino;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1744) data[1] = le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1745) data[2] = inode->i_generation;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1746) *lenp = 3;
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 1747) if (parent) {
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 1748) data[3] = parent->i_ino;
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 1749) data[4] = le32_to_cpu(INODE_PKEY(parent)->k_dir_id);
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 1750) *lenp = 5;
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 1751) if (maxlen >= 6) {
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 1752) data[5] = parent->i_generation;
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 1753) *lenp = 6;
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 1754) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1755) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1756) return *lenp;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1757) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1758)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1759) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1760) * looks for stat data, then copies fields to it, marks the buffer
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1761) * containing stat data as dirty
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1762) */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1763) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1764) * reiserfs inodes are never really dirty, since the dirty inode call
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1765) * always logs them. This call allows the VFS inode marking routines
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1766) * to properly mark inodes for datasync and such, but only actually
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1767) * does something when called for a synchronous update.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1768) */
a9185b41a4f84 (Christoph Hellwig 2010-03-05 09:21:37 +0100 1769) int reiserfs_write_inode(struct inode *inode, struct writeback_control *wbc)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1770) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1771) struct reiserfs_transaction_handle th;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1772) int jbegin_count = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1773)
bc98a42c1f7d0 (David Howells 2017-07-17 08:45:34 +0100 1774) if (sb_rdonly(inode->i_sb))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1775) return -EROFS;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1776) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1777) * memory pressure can sometimes initiate write_inode calls with
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1778) * sync == 1,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1779) * these cases are just when the system needs ram, not when the
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1780) * inode needs to reach disk for safety, and they can safely be
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1781) * ignored because the altered inode has already been logged.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1782) */
a9185b41a4f84 (Christoph Hellwig 2010-03-05 09:21:37 +0100 1783) if (wbc->sync_mode == WB_SYNC_ALL && !(current->flags & PF_MEMALLOC)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1784) reiserfs_write_lock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1785) if (!journal_begin(&th, inode->i_sb, jbegin_count)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1786) reiserfs_update_sd(&th, inode);
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 1787) journal_end_sync(&th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1788) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1789) reiserfs_write_unlock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1790) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1791) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1792) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1793)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1794) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1795) * stat data of new object is inserted already, this inserts the item
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1796) * containing "." and ".." entries
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1797) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1798) static int reiserfs_new_directory(struct reiserfs_transaction_handle *th,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1799) struct inode *inode,
fec6d055da71f (Josef "Jeff" Sipek 2006-12-08 02:36:32 -0800 1800) struct item_head *ih, struct treepath *path,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1801) struct inode *dir)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1802) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1803) struct super_block *sb = th->t_super;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1804) char empty_dir[EMPTY_DIR_SIZE];
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1805) char *body = empty_dir;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1806) struct cpu_key key;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1807) int retval;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1808)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1809) BUG_ON(!th->t_trans_id);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1810)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1811) _make_cpu_key(&key, KEY_FORMAT_3_5, le32_to_cpu(ih->ih_key.k_dir_id),
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1812) le32_to_cpu(ih->ih_key.k_objectid), DOT_OFFSET,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1813) TYPE_DIRENTRY, 3 /*key length */ );
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1814)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1815) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1816) * compose item head for new item. Directories consist of items of
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1817) * old type (ITEM_VERSION_1). Do not set key (second arg is 0), it
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1818) * is done by reiserfs_new_inode
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1819) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1820) if (old_format_only(sb)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1821) make_le_item_head(ih, NULL, KEY_FORMAT_3_5, DOT_OFFSET,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1822) TYPE_DIRENTRY, EMPTY_DIR_SIZE_V1, 2);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1823)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1824) make_empty_dir_item_v1(body, ih->ih_key.k_dir_id,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1825) ih->ih_key.k_objectid,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1826) INODE_PKEY(dir)->k_dir_id,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1827) INODE_PKEY(dir)->k_objectid);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1828) } else {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1829) make_le_item_head(ih, NULL, KEY_FORMAT_3_5, DOT_OFFSET,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1830) TYPE_DIRENTRY, EMPTY_DIR_SIZE, 2);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1831)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1832) make_empty_dir_item(body, ih->ih_key.k_dir_id,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1833) ih->ih_key.k_objectid,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1834) INODE_PKEY(dir)->k_dir_id,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1835) INODE_PKEY(dir)->k_objectid);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1836) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1837)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1838) /* look for place in the tree for new item */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1839) retval = search_item(sb, &key, path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1840) if (retval == IO_ERROR) {
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 1841) reiserfs_error(sb, "vs-13080",
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 1842) "i/o failure occurred creating new directory");
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1843) return -EIO;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1844) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1845) if (retval == ITEM_FOUND) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1846) pathrelse(path);
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 1847) reiserfs_warning(sb, "vs-13070",
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1848) "object with this key exists (%k)",
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1849) &(ih->ih_key));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1850) return -EEXIST;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1851) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1852)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1853) /* insert item, that is empty directory item */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1854) return reiserfs_insert_item(th, path, &key, ih, inode, body);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1855) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1856)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1857) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1858) * stat data of object has been inserted, this inserts the item
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1859) * containing the body of symlink
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1860) */
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1861) static int reiserfs_new_symlink(struct reiserfs_transaction_handle *th,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1862) struct inode *inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1863) struct item_head *ih,
fec6d055da71f (Josef "Jeff" Sipek 2006-12-08 02:36:32 -0800 1864) struct treepath *path, const char *symname,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1865) int item_len)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1866) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1867) struct super_block *sb = th->t_super;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1868) struct cpu_key key;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1869) int retval;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1870)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1871) BUG_ON(!th->t_trans_id);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1872)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1873) _make_cpu_key(&key, KEY_FORMAT_3_5,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1874) le32_to_cpu(ih->ih_key.k_dir_id),
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1875) le32_to_cpu(ih->ih_key.k_objectid),
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1876) 1, TYPE_DIRECT, 3 /*key length */ );
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1877)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1878) make_le_item_head(ih, NULL, KEY_FORMAT_3_5, 1, TYPE_DIRECT, item_len,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1879) 0 /*free_space */ );
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1880)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1881) /* look for place in the tree for new item */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1882) retval = search_item(sb, &key, path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1883) if (retval == IO_ERROR) {
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 1884) reiserfs_error(sb, "vs-13080",
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 1885) "i/o failure occurred creating new symlink");
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1886) return -EIO;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1887) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1888) if (retval == ITEM_FOUND) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1889) pathrelse(path);
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 1890) reiserfs_warning(sb, "vs-13080",
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1891) "object with this key exists (%k)",
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1892) &(ih->ih_key));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1893) return -EEXIST;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1894) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1895)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1896) /* insert item, that is body of symlink */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1897) return reiserfs_insert_item(th, path, &key, ih, inode, symname);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1898) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1899)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1900) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1901) * inserts the stat data into the tree, and then calls
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1902) * reiserfs_new_directory (to insert ".", ".." item if new object is
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1903) * directory) or reiserfs_new_symlink (to insert symlink body if new
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1904) * object is symlink) or nothing (if new object is regular file)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1905)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1906) * NOTE! uid and gid must already be set in the inode. If we return
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1907) * non-zero due to an error, we have to drop the quota previously allocated
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1908) * for the fresh inode. This can only be done outside a transaction, so
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1909) * if we return non-zero, we also end the transaction.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1910) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1911) * @th: active transaction handle
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1912) * @dir: parent directory for new inode
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1913) * @mode: mode of new inode
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1914) * @symname: symlink contents if inode is symlink
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1915) * @isize: 0 for regular file, EMPTY_DIR_SIZE for dirs, strlen(symname) for
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1916) * symlinks
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1917) * @inode: inode to be filled
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1918) * @security: optional security context to associate with this inode
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1919) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1920) int reiserfs_new_inode(struct reiserfs_transaction_handle *th,
8e0718924e7d7 (Al Viro 2011-07-26 02:50:53 -0400 1921) struct inode *dir, umode_t mode, const char *symname,
0222e6571c332 (Jeff Mahoney 2009-03-30 14:02:44 -0400 1922) /* 0 for regular, EMTRY_DIR_SIZE for dirs,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1923) strlen (symname) for symlinks) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1924) loff_t i_size, struct dentry *dentry,
57fe60df62410 (Jeff Mahoney 2009-03-30 14:02:41 -0400 1925) struct inode *inode,
57fe60df62410 (Jeff Mahoney 2009-03-30 14:02:41 -0400 1926) struct reiserfs_security_handle *security)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1927) {
d2d0395fd1778 (Jeff Mahoney 2013-08-08 17:34:47 -0400 1928) struct super_block *sb = dir->i_sb;
c1eaa26b67129 (Al Viro 2008-12-30 02:03:58 -0500 1929) struct reiserfs_iget_args args;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1930) INITIALIZE_PATH(path_to_key);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1931) struct cpu_key key;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1932) struct item_head ih;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1933) struct stat_data sd;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1934) int retval;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1935) int err;
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 1936) int depth;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1937)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1938) BUG_ON(!th->t_trans_id);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1939)
d2d0395fd1778 (Jeff Mahoney 2013-08-08 17:34:47 -0400 1940) depth = reiserfs_write_unlock_nested(sb);
63936ddaa16b9 (Christoph Hellwig 2010-03-03 09:05:01 -0500 1941) err = dquot_alloc_inode(inode);
d2d0395fd1778 (Jeff Mahoney 2013-08-08 17:34:47 -0400 1942) reiserfs_write_lock_nested(sb, depth);
63936ddaa16b9 (Christoph Hellwig 2010-03-03 09:05:01 -0500 1943) if (err)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1944) goto out_end_trans;
585b7747d63e3 (Eric Sesterhenn 2006-10-04 02:15:30 -0700 1945) if (!dir->i_nlink) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1946) err = -EPERM;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1947) goto out_bad_inode;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1948) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1949)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1950) /* item head of new item */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1951) ih.ih_key.k_dir_id = reiserfs_choose_packing(dir);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1952) ih.ih_key.k_objectid = cpu_to_le32(reiserfs_get_unused_objectid(th));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1953) if (!ih.ih_key.k_objectid) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1954) err = -ENOMEM;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1955) goto out_bad_inode;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1956) }
c1eaa26b67129 (Al Viro 2008-12-30 02:03:58 -0500 1957) args.objectid = inode->i_ino = le32_to_cpu(ih.ih_key.k_objectid);
2f1169e2dc0c7 (Al Viro 2009-01-02 08:16:51 -0500 1958) if (old_format_only(sb))
2f1169e2dc0c7 (Al Viro 2009-01-02 08:16:51 -0500 1959) make_le_item_head(&ih, NULL, KEY_FORMAT_3_5, SD_OFFSET,
2f1169e2dc0c7 (Al Viro 2009-01-02 08:16:51 -0500 1960) TYPE_STAT_DATA, SD_V1_SIZE, MAX_US_INT);
2f1169e2dc0c7 (Al Viro 2009-01-02 08:16:51 -0500 1961) else
2f1169e2dc0c7 (Al Viro 2009-01-02 08:16:51 -0500 1962) make_le_item_head(&ih, NULL, KEY_FORMAT_3_6, SD_OFFSET,
2f1169e2dc0c7 (Al Viro 2009-01-02 08:16:51 -0500 1963) TYPE_STAT_DATA, SD_SIZE, MAX_US_INT);
a228bf8f0a3e5 (Jeff Mahoney 2014-04-23 10:00:42 -0400 1964) memcpy(INODE_PKEY(inode), &ih.ih_key, KEY_SIZE);
c1eaa26b67129 (Al Viro 2008-12-30 02:03:58 -0500 1965) args.dirid = le32_to_cpu(ih.ih_key.k_dir_id);
a1457c0ce976b (Jeff Mahoney 2013-05-31 15:51:17 -0400 1966)
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 1967) depth = reiserfs_write_unlock_nested(inode->i_sb);
a1457c0ce976b (Jeff Mahoney 2013-05-31 15:51:17 -0400 1968) err = insert_inode_locked4(inode, args.objectid,
a1457c0ce976b (Jeff Mahoney 2013-05-31 15:51:17 -0400 1969) reiserfs_find_actor, &args);
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 1970) reiserfs_write_lock_nested(inode->i_sb, depth);
a1457c0ce976b (Jeff Mahoney 2013-05-31 15:51:17 -0400 1971) if (err) {
c1eaa26b67129 (Al Viro 2008-12-30 02:03:58 -0500 1972) err = -EINVAL;
c1eaa26b67129 (Al Viro 2008-12-30 02:03:58 -0500 1973) goto out_bad_inode;
c1eaa26b67129 (Al Viro 2008-12-30 02:03:58 -0500 1974) }
a1457c0ce976b (Jeff Mahoney 2013-05-31 15:51:17 -0400 1975)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1976) if (old_format_only(sb))
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1977) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1978) * not a perfect generation count, as object ids can be reused,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1979) * but this is as good as reiserfs can do right now.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1980) * note that the private part of inode isn't filled in yet,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 1981) * we have to use the directory.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1982) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1983) inode->i_generation = le32_to_cpu(INODE_PKEY(dir)->k_objectid);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1984) else
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1985) #if defined( USE_INODE_GENERATION_COUNTER )
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1986) inode->i_generation =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1987) le32_to_cpu(REISERFS_SB(sb)->s_rs->s_inode_generation);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1988) #else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1989) inode->i_generation = ++event;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1990) #endif
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1991)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1992) /* fill stat data */
bfe8684869601 (Miklos Szeredi 2011-10-28 14:13:29 +0200 1993) set_nlink(inode, (S_ISDIR(mode) ? 2 : 1));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1994)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1995) /* uid and gid must already be set by the caller for quota init */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1996)
02027d42c3f74 (Deepa Dinamani 2016-09-14 07:48:05 -0700 1997) inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1998) inode->i_size = i_size;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 1999) inode->i_blocks = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2000) inode->i_bytes = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2001) REISERFS_I(inode)->i_first_direct_byte = S_ISLNK(mode) ? 1 :
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2002) U32_MAX /*NO_BYTES_IN_DIRECT_ITEM */ ;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2003)
a228bf8f0a3e5 (Jeff Mahoney 2014-04-23 10:00:42 -0400 2004) INIT_LIST_HEAD(&REISERFS_I(inode)->i_prealloc_list);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2005) REISERFS_I(inode)->i_flags = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2006) REISERFS_I(inode)->i_prealloc_block = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2007) REISERFS_I(inode)->i_prealloc_count = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2008) REISERFS_I(inode)->i_trans_id = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2009) REISERFS_I(inode)->i_jl = NULL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2010) REISERFS_I(inode)->i_attrs =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2011) REISERFS_I(dir)->i_attrs & REISERFS_INHERIT_MASK;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2012) sd_attrs_to_i_attrs(REISERFS_I(inode)->i_attrs, inode);
068fbb315dd1e (Alexey Dobriyan 2006-09-29 01:59:58 -0700 2013) reiserfs_init_xattr_rwsem(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2014)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2015) /* key to search for correct place for new stat data */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2016) _make_cpu_key(&key, KEY_FORMAT_3_6, le32_to_cpu(ih.ih_key.k_dir_id),
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2017) le32_to_cpu(ih.ih_key.k_objectid), SD_OFFSET,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2018) TYPE_STAT_DATA, 3 /*key length */ );
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2019)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2020) /* find proper place for inserting of stat data */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2021) retval = search_item(sb, &key, &path_to_key);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2022) if (retval == IO_ERROR) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2023) err = -EIO;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2024) goto out_bad_inode;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2025) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2026) if (retval == ITEM_FOUND) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2027) pathrelse(&path_to_key);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2028) err = -EEXIST;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2029) goto out_bad_inode;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2030) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2031) if (old_format_only(sb)) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2032) /* i_uid or i_gid is too big to be stored in stat data v3.5 */
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 2033) if (i_uid_read(inode) & ~0xffff || i_gid_read(inode) & ~0xffff) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2034) pathrelse(&path_to_key);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2035) err = -EINVAL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2036) goto out_bad_inode;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2037) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2038) inode2sd_v1(&sd, inode, inode->i_size);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2039) } else {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2040) inode2sd(&sd, inode, inode->i_size);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2041) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2042) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2043) * store in in-core inode the key of stat data and version all
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2044) * object items will have (directory items will have old offset
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2045) * format, other new objects will consist of new items)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2046) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2047) if (old_format_only(sb) || S_ISDIR(mode) || S_ISLNK(mode))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2048) set_inode_item_key_version(inode, KEY_FORMAT_3_5);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2049) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2050) set_inode_item_key_version(inode, KEY_FORMAT_3_6);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2051) if (old_format_only(sb))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2052) set_inode_sd_version(inode, STAT_DATA_V1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2053) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2054) set_inode_sd_version(inode, STAT_DATA_V2);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2055)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2056) /* insert the stat data into the tree */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2057) #ifdef DISPLACE_NEW_PACKING_LOCALITIES
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2058) if (REISERFS_I(dir)->new_packing_locality)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2059) th->displace_new_blocks = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2060) #endif
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2061) retval =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2062) reiserfs_insert_item(th, &path_to_key, &key, &ih, inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2063) (char *)(&sd));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2064) if (retval) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2065) err = retval;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2066) reiserfs_check_path(&path_to_key);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2067) goto out_bad_inode;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2068) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2069) #ifdef DISPLACE_NEW_PACKING_LOCALITIES
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2070) if (!th->displace_new_blocks)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2071) REISERFS_I(dir)->new_packing_locality = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2072) #endif
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2073) if (S_ISDIR(mode)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2074) /* insert item with "." and ".." */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2075) retval =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2076) reiserfs_new_directory(th, inode, &ih, &path_to_key, dir);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2077) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2078)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2079) if (S_ISLNK(mode)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2080) /* insert body of symlink */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2081) if (!old_format_only(sb))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2082) i_size = ROUND_UP(i_size);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2083) retval =
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2084) reiserfs_new_symlink(th, inode, &ih, &path_to_key, symname,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2085) i_size);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2086) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2087) if (retval) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2088) err = retval;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2089) reiserfs_check_path(&path_to_key);
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 2090) journal_end(th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2091) goto out_inserted_sd;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2092) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2093)
60e4cf67a582d (Jeff Mahoney 2019-10-24 10:31:27 -0400 2094) /*
60e4cf67a582d (Jeff Mahoney 2019-10-24 10:31:27 -0400 2095) * Mark it private if we're creating the privroot
60e4cf67a582d (Jeff Mahoney 2019-10-24 10:31:27 -0400 2096) * or something under it.
60e4cf67a582d (Jeff Mahoney 2019-10-24 10:31:27 -0400 2097) */
60e4cf67a582d (Jeff Mahoney 2019-10-24 10:31:27 -0400 2098) if (IS_PRIVATE(dir) || dentry == REISERFS_SB(sb)->priv_root) {
60e4cf67a582d (Jeff Mahoney 2019-10-24 10:31:27 -0400 2099) inode->i_flags |= S_PRIVATE;
60e4cf67a582d (Jeff Mahoney 2019-10-24 10:31:27 -0400 2100) inode->i_opflags &= ~IOP_XATTR;
60e4cf67a582d (Jeff Mahoney 2019-10-24 10:31:27 -0400 2101) }
60e4cf67a582d (Jeff Mahoney 2019-10-24 10:31:27 -0400 2102)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2103) if (reiserfs_posixacl(inode->i_sb)) {
4c05141df57f4 (Jeff Mahoney 2013-08-08 17:27:19 -0400 2104) reiserfs_write_unlock(inode->i_sb);
0ab2621ebd9a2 (Jeff Mahoney 2009-03-30 14:02:39 -0400 2105) retval = reiserfs_inherit_default_acl(th, dir, dentry, inode);
4c05141df57f4 (Jeff Mahoney 2013-08-08 17:27:19 -0400 2106) reiserfs_write_lock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2107) if (retval) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2108) err = retval;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2109) reiserfs_check_path(&path_to_key);
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 2110) journal_end(th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2111) goto out_inserted_sd;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2112) }
1751e8a6cb935 (Linus Torvalds 2017-11-27 13:05:09 -0800 2113) } else if (inode->i_sb->s_flags & SB_POSIXACL) {
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 2114) reiserfs_warning(inode->i_sb, "jdm-13090",
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 2115) "ACLs aren't enabled in the fs, "
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2116) "but vfs thinks they are!");
60e4cf67a582d (Jeff Mahoney 2019-10-24 10:31:27 -0400 2117) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2118)
57fe60df62410 (Jeff Mahoney 2009-03-30 14:02:41 -0400 2119) if (security->name) {
4c05141df57f4 (Jeff Mahoney 2013-08-08 17:27:19 -0400 2120) reiserfs_write_unlock(inode->i_sb);
57fe60df62410 (Jeff Mahoney 2009-03-30 14:02:41 -0400 2121) retval = reiserfs_security_write(th, inode, security);
4c05141df57f4 (Jeff Mahoney 2013-08-08 17:27:19 -0400 2122) reiserfs_write_lock(inode->i_sb);
57fe60df62410 (Jeff Mahoney 2009-03-30 14:02:41 -0400 2123) if (retval) {
57fe60df62410 (Jeff Mahoney 2009-03-30 14:02:41 -0400 2124) err = retval;
57fe60df62410 (Jeff Mahoney 2009-03-30 14:02:41 -0400 2125) reiserfs_check_path(&path_to_key);
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 2126) retval = journal_end(th);
57fe60df62410 (Jeff Mahoney 2009-03-30 14:02:41 -0400 2127) if (retval)
57fe60df62410 (Jeff Mahoney 2009-03-30 14:02:41 -0400 2128) err = retval;
57fe60df62410 (Jeff Mahoney 2009-03-30 14:02:41 -0400 2129) goto out_inserted_sd;
57fe60df62410 (Jeff Mahoney 2009-03-30 14:02:41 -0400 2130) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2131) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2132)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2133) reiserfs_update_sd(th, inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2134) reiserfs_check_path(&path_to_key);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2135)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2136) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2137)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2138) out_bad_inode:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2139) /* Invalidate the object, nothing was inserted yet */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2140) INODE_PKEY(inode)->k_objectid = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2141)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2142) /* Quota change must be inside a transaction for journaling */
d2d0395fd1778 (Jeff Mahoney 2013-08-08 17:34:47 -0400 2143) depth = reiserfs_write_unlock_nested(inode->i_sb);
63936ddaa16b9 (Christoph Hellwig 2010-03-03 09:05:01 -0500 2144) dquot_free_inode(inode);
d2d0395fd1778 (Jeff Mahoney 2013-08-08 17:34:47 -0400 2145) reiserfs_write_lock_nested(inode->i_sb, depth);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2146)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2147) out_end_trans:
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 2148) journal_end(th);
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2149) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2150) * Drop can be outside and it needs more credits so it's better
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2151) * to have it outside
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2152) */
d2d0395fd1778 (Jeff Mahoney 2013-08-08 17:34:47 -0400 2153) depth = reiserfs_write_unlock_nested(inode->i_sb);
9f7547580263d (Christoph Hellwig 2010-03-03 09:05:05 -0500 2154) dquot_drop(inode);
d2d0395fd1778 (Jeff Mahoney 2013-08-08 17:34:47 -0400 2155) reiserfs_write_lock_nested(inode->i_sb, depth);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2156) inode->i_flags |= S_NOQUOTA;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2157) make_bad_inode(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2158)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2159) out_inserted_sd:
6d6b77f163c7e (Miklos Szeredi 2011-10-28 14:13:28 +0200 2160) clear_nlink(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2161) th->t_trans_id = 0; /* so the caller can't use this handle later */
8859bf2b1278d (Eric Biggers 2020-06-28 00:00:57 -0700 2162) if (inode->i_state & I_NEW)
8859bf2b1278d (Eric Biggers 2020-06-28 00:00:57 -0700 2163) unlock_new_inode(inode);
d984561b326cd (Jeff Mahoney 2009-03-30 14:02:35 -0400 2164) iput(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2165) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2166) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2167)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2168) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2169) * finds the tail page in the page cache,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2170) * reads the last block in.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2171) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2172) * On success, page_result is set to a locked, pinned page, and bh_result
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2173) * is set to an up to date buffer for the last block in the file. returns 0.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2174) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2175) * tail conversion is not done, so bh_result might not be valid for writing
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2176) * check buffer_mapped(bh_result) and bh_result->b_blocknr != 0 before
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2177) * trying to write the block.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2178) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2179) * on failure, nonzero is returned, page_result and bh_result are untouched.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2180) */
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2181) static int grab_tail_page(struct inode *inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2182) struct page **page_result,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2183) struct buffer_head **bh_result)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2184) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2185)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2186) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2187) * we want the page with the last byte in the file,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2188) * not the page that will hold the next byte for appending
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2189) */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2190) unsigned long index = (inode->i_size - 1) >> PAGE_SHIFT;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2191) unsigned long pos = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2192) unsigned long start = 0;
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2193) unsigned long blocksize = inode->i_sb->s_blocksize;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2194) unsigned long offset = (inode->i_size) & (PAGE_SIZE - 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2195) struct buffer_head *bh;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2196) struct buffer_head *head;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2197) struct page *page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2198) int error;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2199)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2200) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2201) * we know that we are only called with inode->i_size > 0.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2202) * we also know that a file tail can never be as big as a block
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2203) * If i_size % blocksize == 0, our file is currently block aligned
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2204) * and it won't need converting or zeroing after a truncate.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2205) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2206) if ((offset & (blocksize - 1)) == 0) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2207) return -ENOENT;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2208) }
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2209) page = grab_cache_page(inode->i_mapping, index);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2210) error = -ENOMEM;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2211) if (!page) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2212) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2213) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2214) /* start within the page of the last block in the file */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2215) start = (offset / blocksize) * blocksize;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2216)
ebdec241d509c (Christoph Hellwig 2010-10-06 10:47:23 +0200 2217) error = __block_write_begin(page, start, offset - start,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2218) reiserfs_get_block_create_0);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2219) if (error)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2220) goto unlock;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2221)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2222) head = page_buffers(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2223) bh = head;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2224) do {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2225) if (pos >= start) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2226) break;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2227) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2228) bh = bh->b_this_page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2229) pos += blocksize;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2230) } while (bh != head);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2231)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2232) if (!buffer_uptodate(bh)) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2233) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2234) * note, this should never happen, prepare_write should be
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2235) * taking care of this for us. If the buffer isn't up to
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2236) * date, I've screwed up the code to find the buffer, or the
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2237) * code to call prepare_write
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2238) */
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2239) reiserfs_error(inode->i_sb, "clm-6000",
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 2240) "error reading block %lu", bh->b_blocknr);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2241) error = -EIO;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2242) goto unlock;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2243) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2244) *bh_result = bh;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2245) *page_result = page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2246)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2247) out:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2248) return error;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2249)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2250) unlock:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2251) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2252) put_page(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2253) return error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2254) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2255)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2256) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2257) * vfs version of truncate file. Must NOT be called with
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2258) * a transaction already started.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2259) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2260) * some code taken from block_truncate_page
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2261) */
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2262) int reiserfs_truncate_file(struct inode *inode, int update_timestamps)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2263) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2264) struct reiserfs_transaction_handle th;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2265) /* we want the offset for the first byte after the end of the file */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2266) unsigned long offset = inode->i_size & (PAGE_SIZE - 1);
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2267) unsigned blocksize = inode->i_sb->s_blocksize;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2268) unsigned length;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2269) struct page *page = NULL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2270) int error;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2271) struct buffer_head *bh = NULL;
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 2272) int err2;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2273)
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 2274) reiserfs_write_lock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2275)
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2276) if (inode->i_size > 0) {
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2277) error = grab_tail_page(inode, &page, &bh);
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2278) if (error) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2279) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2280) * -ENOENT means we truncated past the end of the
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2281) * file, and get_block_create_0 could not find a
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2282) * block to read in, which is ok.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2283) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2284) if (error != -ENOENT)
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2285) reiserfs_error(inode->i_sb, "clm-6001",
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 2286) "grab_tail_page failed %d",
0030b64570c86 (Jeff Mahoney 2009-03-30 14:02:28 -0400 2287) error);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2288) page = NULL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2289) bh = NULL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2290) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2291) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2292)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2293) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2294) * so, if page != NULL, we have a buffer head for the offset at
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2295) * the end of the file. if the bh is mapped, and bh->b_blocknr != 0,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2296) * then we have an unformatted node. Otherwise, we have a direct item,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2297) * and no zeroing is required on disk. We zero after the truncate,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2298) * because the truncate might pack the item anyway
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2299) * (it will unmap bh if it packs).
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2300) *
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2301) * it is enough to reserve space in transaction for 2 balancings:
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2302) * one for "save" link adding and another for the first
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2303) * cut_from_item. 1 is for update_sd
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2304) */
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2305) error = journal_begin(&th, inode->i_sb,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2306) JOURNAL_PER_BALANCE_CNT * 2 + 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2307) if (error)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2308) goto out;
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2309) reiserfs_update_inode_transaction(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2310) if (update_timestamps)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2311) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2312) * we are doing real truncate: if the system crashes
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2313) * before the last transaction of truncating gets committed
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2314) * - on reboot the file either appears truncated properly
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2315) * or not truncated at all
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2316) */
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2317) add_save_link(&th, inode, 1);
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2318) err2 = reiserfs_do_truncate(&th, inode, page, update_timestamps);
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 2319) error = journal_end(&th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2320) if (error)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2321) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2322)
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 2323) /* check reiserfs_do_truncate after ending the transaction */
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 2324) if (err2) {
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 2325) error = err2;
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 2326) goto out;
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 2327) }
2499604960fff (Jeff Mahoney 2005-12-14 14:38:05 -0500 2328)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2329) if (update_timestamps) {
995c762ea486b (Jeff Mahoney 2009-03-30 14:02:47 -0400 2330) error = remove_save_link(inode, 1 /* truncate */);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2331) if (error)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2332) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2333) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2334)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2335) if (page) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2336) length = offset & (blocksize - 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2337) /* if we are not on a block boundary */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2338) if (length) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2339) length = blocksize - length;
eebd2aa355692 (Christoph Lameter 2008-02-04 22:28:29 -0800 2340) zero_user(page, offset, length);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2341) if (buffer_mapped(bh) && bh->b_blocknr != 0) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2342) mark_buffer_dirty(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2343) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2344) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2345) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2346) put_page(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2347) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2348)
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 2349) reiserfs_write_unlock(inode->i_sb);
22c963addcf42 (Frederic Weisbecker 2009-04-14 05:34:24 +0200 2350)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2351) return 0;
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2352) out:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2353) if (page) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2354) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2355) put_page(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2356) }
22c963addcf42 (Frederic Weisbecker 2009-04-14 05:34:24 +0200 2357)
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 2358) reiserfs_write_unlock(inode->i_sb);
22c963addcf42 (Frederic Weisbecker 2009-04-14 05:34:24 +0200 2359)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2360) return error;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2361) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2362)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2363) static int map_block_for_writepage(struct inode *inode,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2364) struct buffer_head *bh_result,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2365) unsigned long block)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2366) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2367) struct reiserfs_transaction_handle th;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2368) int fs_gen;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2369) struct item_head tmp_ih;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2370) struct item_head *ih;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2371) struct buffer_head *bh;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2372) __le32 *item;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2373) struct cpu_key key;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2374) INITIALIZE_PATH(path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2375) int pos_in_item;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2376) int jbegin_count = JOURNAL_PER_BALANCE_CNT;
7729ac5efe156 (Oleg Drokin 2005-11-28 13:43:53 -0800 2377) loff_t byte_offset = ((loff_t)block << inode->i_sb->s_blocksize_bits)+1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2378) int retval;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2379) int use_get_block = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2380) int bytes_copied = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2381) int copy_size;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2382) int trans_running = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2383)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2384) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2385) * catch places below that try to log something without
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2386) * starting a trans
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2387) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2388) th.t_trans_id = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2389)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2390) if (!buffer_uptodate(bh_result)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2391) return -EIO;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2392) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2393)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2394) kmap(bh_result->b_page);
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2395) start_over:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2396) reiserfs_write_lock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2397) make_cpu_key(&key, inode, byte_offset, TYPE_ANY, 3);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2398)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2399) research:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2400) retval = search_for_position_by_key(inode->i_sb, &key, &path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2401) if (retval != POSITION_FOUND) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2402) use_get_block = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2403) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2404) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2405)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2406) bh = get_last_bh(&path);
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 2407) ih = tp_item_head(&path);
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 2408) item = tp_item_body(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2409) pos_in_item = path.pos_in_item;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2410)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2411) /* we've found an unformatted node */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2412) if (indirect_item_found(retval, ih)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2413) if (bytes_copied > 0) {
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 2414) reiserfs_warning(inode->i_sb, "clm-6002",
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 2415) "bytes_copied %d", bytes_copied);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2416) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2417) if (!get_block_num(item, pos_in_item)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2418) /* crap, we are writing to a hole */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2419) use_get_block = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2420) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2421) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2422) set_block_dev_mapped(bh_result,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2423) get_block_num(item, pos_in_item), inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2424) } else if (is_direct_le_ih(ih)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2425) char *p;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2426) p = page_address(bh_result->b_page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2427) p += (byte_offset - 1) & (PAGE_SIZE - 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2428) copy_size = ih_item_len(ih) - pos_in_item;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2429)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2430) fs_gen = get_generation(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2431) copy_item_head(&tmp_ih, ih);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2432)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2433) if (!trans_running) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2434) /* vs-3050 is gone, no need to drop the path */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2435) retval = journal_begin(&th, inode->i_sb, jbegin_count);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2436) if (retval)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2437) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2438) reiserfs_update_inode_transaction(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2439) trans_running = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2440) if (fs_changed(fs_gen, inode->i_sb)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2441) && item_moved(&tmp_ih, &path)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2442) reiserfs_restore_prepared_buffer(inode->i_sb,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2443) bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2444) goto research;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2445) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2446) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2447)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2448) reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2449)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2450) if (fs_changed(fs_gen, inode->i_sb)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2451) && item_moved(&tmp_ih, &path)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2452) reiserfs_restore_prepared_buffer(inode->i_sb, bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2453) goto research;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2454) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2455)
4cf5f7addf18e (Jeff Mahoney 2014-04-23 10:00:35 -0400 2456) memcpy(ih_item_body(bh, ih) + pos_in_item, p + bytes_copied,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2457) copy_size);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2458)
09f1b80ba8c96 (Jeff Mahoney 2014-04-23 10:00:39 -0400 2459) journal_mark_dirty(&th, bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2460) bytes_copied += copy_size;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2461) set_block_dev_mapped(bh_result, 0, inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2462)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2463) /* are there still bytes left? */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2464) if (bytes_copied < bh_result->b_size &&
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2465) (byte_offset + bytes_copied) < inode->i_size) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2466) set_cpu_key_k_offset(&key,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2467) cpu_key_k_offset(&key) +
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2468) copy_size);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2469) goto research;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2470) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2471) } else {
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 2472) reiserfs_warning(inode->i_sb, "clm-6003",
45b03d5e8e674 (Jeff Mahoney 2009-03-30 14:02:21 -0400 2473) "bad item inode %lu", inode->i_ino);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2474) retval = -EIO;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2475) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2476) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2477) retval = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2478)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2479) out:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2480) pathrelse(&path);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2481) if (trans_running) {
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 2482) int err = journal_end(&th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2483) if (err)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2484) retval = err;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2485) trans_running = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2486) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2487) reiserfs_write_unlock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2488)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2489) /* this is where we fill in holes in the file. */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2490) if (use_get_block) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2491) retval = reiserfs_get_block(inode, block, bh_result,
1b1dcc1b57a49 (Jes Sorensen 2006-01-09 15:59:24 -0800 2492) GET_BLOCK_CREATE | GET_BLOCK_NO_IMUX
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2493) | GET_BLOCK_NO_DANGLE);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2494) if (!retval) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2495) if (!buffer_mapped(bh_result)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2496) || bh_result->b_blocknr == 0) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2497) /* get_block failed to find a mapped unformatted node. */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2498) use_get_block = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2499) goto start_over;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2500) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2501) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2502) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2503) kunmap(bh_result->b_page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2504)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2505) if (!retval && buffer_mapped(bh_result) && bh_result->b_blocknr == 0) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2506) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2507) * we've copied data from the page into the direct item, so the
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2508) * buffer in the page is now clean, mark it to reflect that.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2509) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2510) lock_buffer(bh_result);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2511) clear_buffer_dirty(bh_result);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2512) unlock_buffer(bh_result);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2513) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2514) return retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2515) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2516)
0222e6571c332 (Jeff Mahoney 2009-03-30 14:02:44 -0400 2517) /*
0222e6571c332 (Jeff Mahoney 2009-03-30 14:02:44 -0400 2518) * mason@suse.com: updated in 2.5.54 to follow the same general io
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2519) * start/recovery path as __block_write_full_page, along with special
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2520) * code to handle reiserfs tails.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2521) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2522) static int reiserfs_write_full_page(struct page *page,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2523) struct writeback_control *wbc)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2524) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2525) struct inode *inode = page->mapping->host;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2526) unsigned long end_index = inode->i_size >> PAGE_SHIFT;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2527) int error = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2528) unsigned long block;
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2529) sector_t last_block;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2530) struct buffer_head *head, *bh;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2531) int partial = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2532) int nr = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2533) int checked = PageChecked(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2534) struct reiserfs_transaction_handle th;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2535) struct super_block *s = inode->i_sb;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2536) int bh_per_page = PAGE_SIZE / s->s_blocksize;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2537) th.t_trans_id = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2538)
e0e851cf30f1a (Chris Mason 2006-02-01 03:06:49 -0800 2539) /* no logging allowed when nonblocking or from PF_MEMALLOC */
e0e851cf30f1a (Chris Mason 2006-02-01 03:06:49 -0800 2540) if (checked && (current->flags & PF_MEMALLOC)) {
e0e851cf30f1a (Chris Mason 2006-02-01 03:06:49 -0800 2541) redirty_page_for_writepage(wbc, page);
e0e851cf30f1a (Chris Mason 2006-02-01 03:06:49 -0800 2542) unlock_page(page);
e0e851cf30f1a (Chris Mason 2006-02-01 03:06:49 -0800 2543) return 0;
e0e851cf30f1a (Chris Mason 2006-02-01 03:06:49 -0800 2544) }
e0e851cf30f1a (Chris Mason 2006-02-01 03:06:49 -0800 2545)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2546) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2547) * The page dirty bit is cleared before writepage is called, which
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2548) * means we have to tell create_empty_buffers to make dirty buffers
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2549) * The page really should be up to date at this point, so tossing
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2550) * in the BH_Uptodate is just a sanity check.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2551) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2552) if (!page_has_buffers(page)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2553) create_empty_buffers(page, s->s_blocksize,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2554) (1 << BH_Dirty) | (1 << BH_Uptodate));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2555) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2556) head = page_buffers(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2557)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2558) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2559) * last page in the file, zero out any contents past the
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2560) * last byte in the file
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2561) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2562) if (page->index >= end_index) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2563) unsigned last_offset;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2564)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2565) last_offset = inode->i_size & (PAGE_SIZE - 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2566) /* no file contents in this page */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2567) if (page->index >= end_index + 1 || !last_offset) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2568) unlock_page(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2569) return 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2570) }
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2571) zero_user_segment(page, last_offset, PAGE_SIZE);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2572) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2573) bh = head;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2574) block = page->index << (PAGE_SHIFT - s->s_blocksize_bits);
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2575) last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2576) /* first map all the buffers, logging any direct items we find */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2577) do {
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2578) if (block > last_block) {
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2579) /*
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2580) * This can happen when the block size is less than
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2581) * the page size. The corresponding bytes in the page
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2582) * were zero filled above
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2583) */
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2584) clear_buffer_dirty(bh);
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2585) set_buffer_uptodate(bh);
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2586) } else if ((checked || buffer_dirty(bh)) &&
b4c76fa721c7c (Chris Mason 2006-08-05 12:15:10 -0700 2587) (!buffer_mapped(bh) || (buffer_mapped(bh)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2588) && bh->b_blocknr ==
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2589) 0))) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2590) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2591) * not mapped yet, or it points to a direct item, search
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2592) * the btree for the mapping info, and log any direct
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2593) * items found
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2594) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2595) if ((error = map_block_for_writepage(inode, bh, block))) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2596) goto fail;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2597) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2598) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2599) bh = bh->b_this_page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2600) block++;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2601) } while (bh != head);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2602)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2603) /*
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2604) * we start the transaction after map_block_for_writepage,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2605) * because it can create holes in the file (an unbounded operation).
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2606) * starting it here, we can make a reliable estimate for how many
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2607) * blocks we're going to log
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2608) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2609) if (checked) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2610) ClearPageChecked(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2611) reiserfs_write_lock(s);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2612) error = journal_begin(&th, s, bh_per_page + 1);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2613) if (error) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2614) reiserfs_write_unlock(s);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2615) goto fail;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2616) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2617) reiserfs_update_inode_transaction(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2618) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2619) /* now go through and lock any dirty buffers on the page */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2620) do {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2621) get_bh(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2622) if (!buffer_mapped(bh))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2623) continue;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2624) if (buffer_mapped(bh) && bh->b_blocknr == 0)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2625) continue;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2626)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2627) if (checked) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2628) reiserfs_prepare_for_journal(s, bh, 1);
09f1b80ba8c96 (Jeff Mahoney 2014-04-23 10:00:39 -0400 2629) journal_mark_dirty(&th, bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2630) continue;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2631) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2632) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2633) * from this point on, we know the buffer is mapped to a
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2634) * real block and not a direct item
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2635) */
1b430beee5e38 (Wu Fengguang 2010-10-26 14:21:26 -0700 2636) if (wbc->sync_mode != WB_SYNC_NONE) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2637) lock_buffer(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2638) } else {
ca5de404ff036 (Nicholas Piggin 2008-08-02 12:02:13 +0200 2639) if (!trylock_buffer(bh)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2640) redirty_page_for_writepage(wbc, page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2641) continue;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2642) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2643) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2644) if (test_clear_buffer_dirty(bh)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2645) mark_buffer_async_write(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2646) } else {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2647) unlock_buffer(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2648) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2649) } while ((bh = bh->b_this_page) != head);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2650)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2651) if (checked) {
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 2652) error = journal_end(&th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2653) reiserfs_write_unlock(s);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2654) if (error)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2655) goto fail;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2656) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2657) BUG_ON(PageWriteback(page));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2658) set_page_writeback(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2659) unlock_page(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2660)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2661) /*
0222e6571c332 (Jeff Mahoney 2009-03-30 14:02:44 -0400 2662) * since any buffer might be the only dirty buffer on the page,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2663) * the first submit_bh can bring the page out of writeback.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2664) * be careful with the buffers.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2665) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2666) do {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2667) struct buffer_head *next = bh->b_this_page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2668) if (buffer_async_write(bh)) {
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 2669) submit_bh(REQ_OP_WRITE, 0, bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2670) nr++;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2671) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2672) put_bh(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2673) bh = next;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2674) } while (bh != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2675)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2676) error = 0;
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2677) done:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2678) if (nr == 0) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2679) /*
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2680) * if this page only had a direct item, it is very possible for
0222e6571c332 (Jeff Mahoney 2009-03-30 14:02:44 -0400 2681) * no io to be required without there being an error. Or,
0222e6571c332 (Jeff Mahoney 2009-03-30 14:02:44 -0400 2682) * someone else could have locked them and sent them down the
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2683) * pipe without locking the page
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2684) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2685) bh = head;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2686) do {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2687) if (!buffer_uptodate(bh)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2688) partial = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2689) break;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2690) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2691) bh = bh->b_this_page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2692) } while (bh != head);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2693) if (!partial)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2694) SetPageUptodate(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2695) end_page_writeback(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2696) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2697) return error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2698)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2699) fail:
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2700) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2701) * catches various errors, we need to make sure any valid dirty blocks
0222e6571c332 (Jeff Mahoney 2009-03-30 14:02:44 -0400 2702) * get to the media. The page is currently locked and not marked for
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2703) * writeback
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2704) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2705) ClearPageUptodate(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2706) bh = head;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2707) do {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2708) get_bh(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2709) if (buffer_mapped(bh) && buffer_dirty(bh) && bh->b_blocknr) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2710) lock_buffer(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2711) mark_buffer_async_write(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2712) } else {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2713) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2714) * clear any dirty bits that might have come from
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2715) * getting attached to a dirty page
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2716) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2717) clear_buffer_dirty(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2718) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2719) bh = bh->b_this_page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2720) } while (bh != head);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2721) SetPageError(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2722) BUG_ON(PageWriteback(page));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2723) set_page_writeback(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2724) unlock_page(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2725) do {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2726) struct buffer_head *next = bh->b_this_page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2727) if (buffer_async_write(bh)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2728) clear_buffer_dirty(bh);
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 2729) submit_bh(REQ_OP_WRITE, 0, bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2730) nr++;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2731) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2732) put_bh(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2733) bh = next;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2734) } while (bh != head);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2735) goto done;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2736) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2737)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2738) static int reiserfs_readpage(struct file *f, struct page *page)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2739) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2740) return block_read_full_page(page, reiserfs_get_block);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2741) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2742)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2743) static int reiserfs_writepage(struct page *page, struct writeback_control *wbc)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2744) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2745) struct inode *inode = page->mapping->host;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2746) reiserfs_wait_on_write_block(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2747) return reiserfs_write_full_page(page, wbc);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2748) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2749)
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2750) static void reiserfs_truncate_failed_write(struct inode *inode)
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2751) {
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2752) truncate_inode_pages(inode->i_mapping, inode->i_size);
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2753) reiserfs_truncate_file(inode, 0);
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2754) }
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2755)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2756) static int reiserfs_write_begin(struct file *file,
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2757) struct address_space *mapping,
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2758) loff_t pos, unsigned len, unsigned flags,
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2759) struct page **pagep, void **fsdata)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2760) {
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2761) struct inode *inode;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2762) struct page *page;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2763) pgoff_t index;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2764) int ret;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2765) int old_ref = 0;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2766)
f7557e8f7ff78 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2767) inode = mapping->host;
714b71a3a91f6 (Fabian Frederick 2015-02-17 13:45:33 -0800 2768) *fsdata = NULL;
f7557e8f7ff78 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2769) if (flags & AOP_FLAG_CONT_EXPAND &&
f7557e8f7ff78 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2770) (pos & (inode->i_sb->s_blocksize - 1)) == 0) {
f7557e8f7ff78 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2771) pos ++;
f7557e8f7ff78 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2772) *fsdata = (void *)(unsigned long)flags;
f7557e8f7ff78 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2773) }
f7557e8f7ff78 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2774)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2775) index = pos >> PAGE_SHIFT;
54566b2c1594c (Nicholas Piggin 2009-01-04 12:00:53 -0800 2776) page = grab_cache_page_write_begin(mapping, index, flags);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2777) if (!page)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2778) return -ENOMEM;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2779) *pagep = page;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2780)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2781) reiserfs_wait_on_write_block(inode->i_sb);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2782) fix_tail_page_for_writing(page);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2783) if (reiserfs_transaction_running(inode->i_sb)) {
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2784) struct reiserfs_transaction_handle *th;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2785) th = (struct reiserfs_transaction_handle *)current->
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2786) journal_info;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2787) BUG_ON(!th->t_refcount);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2788) BUG_ON(!th->t_trans_id);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2789) old_ref = th->t_refcount;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2790) th->t_refcount++;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2791) }
6e1db88d536ad (Christoph Hellwig 2010-06-04 11:29:57 +0200 2792) ret = __block_write_begin(page, pos, len, reiserfs_get_block);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2793) if (ret && reiserfs_transaction_running(inode->i_sb)) {
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2794) struct reiserfs_transaction_handle *th = current->journal_info;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2795) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2796) * this gets a little ugly. If reiserfs_get_block returned an
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2797) * error and left a transacstion running, we've got to close
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2798) * it, and we've got to free handle if it was a persistent
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2799) * transaction.
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2800) *
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2801) * But, if we had nested into an existing transaction, we need
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2802) * to just drop the ref count on the handle.
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2803) *
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2804) * If old_ref == 0, the transaction is from reiserfs_get_block,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2805) * and it was a persistent trans. Otherwise, it was nested
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2806) * above.
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2807) */
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2808) if (th->t_refcount > old_ref) {
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2809) if (old_ref)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2810) th->t_refcount--;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2811) else {
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2812) int err;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2813) reiserfs_write_lock(inode->i_sb);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2814) err = reiserfs_end_persistent_transaction(th);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2815) reiserfs_write_unlock(inode->i_sb);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2816) if (err)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2817) ret = err;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2818) }
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2819) }
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2820) }
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2821) if (ret) {
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2822) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2823) put_page(page);
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2824) /* Truncate allocated blocks */
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2825) reiserfs_truncate_failed_write(inode);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2826) }
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2827) return ret;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2828) }
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2829)
ebdec241d509c (Christoph Hellwig 2010-10-06 10:47:23 +0200 2830) int __reiserfs_write_begin(struct page *page, unsigned from, unsigned len)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2831) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2832) struct inode *inode = page->mapping->host;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2833) int ret;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2834) int old_ref = 0;
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 2835) int depth;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2836)
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 2837) depth = reiserfs_write_unlock_nested(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2838) reiserfs_wait_on_write_block(inode->i_sb);
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 2839) reiserfs_write_lock_nested(inode->i_sb, depth);
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 2840)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2841) fix_tail_page_for_writing(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2842) if (reiserfs_transaction_running(inode->i_sb)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2843) struct reiserfs_transaction_handle *th;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2844) th = (struct reiserfs_transaction_handle *)current->
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2845) journal_info;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2846) BUG_ON(!th->t_refcount);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2847) BUG_ON(!th->t_trans_id);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2848) old_ref = th->t_refcount;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2849) th->t_refcount++;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2850) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2851)
ebdec241d509c (Christoph Hellwig 2010-10-06 10:47:23 +0200 2852) ret = __block_write_begin(page, from, len, reiserfs_get_block);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2853) if (ret && reiserfs_transaction_running(inode->i_sb)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2854) struct reiserfs_transaction_handle *th = current->journal_info;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2855) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2856) * this gets a little ugly. If reiserfs_get_block returned an
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2857) * error and left a transacstion running, we've got to close
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2858) * it, and we've got to free handle if it was a persistent
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2859) * transaction.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2860) *
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2861) * But, if we had nested into an existing transaction, we need
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2862) * to just drop the ref count on the handle.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2863) *
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2864) * If old_ref == 0, the transaction is from reiserfs_get_block,
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2865) * and it was a persistent trans. Otherwise, it was nested
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2866) * above.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2867) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2868) if (th->t_refcount > old_ref) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2869) if (old_ref)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2870) th->t_refcount--;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2871) else {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2872) int err;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2873) reiserfs_write_lock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2874) err = reiserfs_end_persistent_transaction(th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2875) reiserfs_write_unlock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2876) if (err)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2877) ret = err;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2878) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2879) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2880) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2881) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2882)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2883) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2884)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2885) static sector_t reiserfs_aop_bmap(struct address_space *as, sector_t block)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2886) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2887) return generic_block_bmap(as, block, reiserfs_bmap);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2888) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2889)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2890) static int reiserfs_write_end(struct file *file, struct address_space *mapping,
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2891) loff_t pos, unsigned len, unsigned copied,
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2892) struct page *page, void *fsdata)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2893) {
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2894) struct inode *inode = page->mapping->host;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2895) int ret = 0;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2896) int update_sd = 0;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2897) struct reiserfs_transaction_handle *th;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2898) unsigned start;
d6f5b0aa08078 (Frederic Weisbecker 2009-05-08 14:53:52 +0200 2899) bool locked = false;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2900)
f7557e8f7ff78 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2901) if ((unsigned long)fsdata & AOP_FLAG_CONT_EXPAND)
f7557e8f7ff78 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2902) pos ++;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2903)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2904) reiserfs_wait_on_write_block(inode->i_sb);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2905) if (reiserfs_transaction_running(inode->i_sb))
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2906) th = current->journal_info;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2907) else
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2908) th = NULL;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2909)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2910) start = pos & (PAGE_SIZE - 1);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2911) if (unlikely(copied < len)) {
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2912) if (!PageUptodate(page))
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2913) copied = 0;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2914)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2915) page_zero_new_buffers(page, start + copied, start + len);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2916) }
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2917) flush_dcache_page(page);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2918)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2919) reiserfs_commit_page(inode, page, start, start + copied);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2920)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2921) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2922) * generic_commit_write does this for us, but does not update the
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2923) * transaction tracking stuff when the size changes. So, we have
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2924) * to do the i_size updates here.
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2925) */
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2926) if (pos + copied > inode->i_size) {
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2927) struct reiserfs_transaction_handle myth;
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 2928) reiserfs_write_lock(inode->i_sb);
d6f5b0aa08078 (Frederic Weisbecker 2009-05-08 14:53:52 +0200 2929) locked = true;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2930) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2931) * If the file have grown beyond the border where it
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2932) * can have a tail, unmark it as needing a tail
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2933) * packing
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2934) */
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2935) if ((have_large_tails(inode->i_sb)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2936) && inode->i_size > i_block_size(inode) * 4)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2937) || (have_small_tails(inode->i_sb)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2938) && inode->i_size > i_block_size(inode)))
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2939) REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2940)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2941) ret = journal_begin(&myth, inode->i_sb, 1);
d6f5b0aa08078 (Frederic Weisbecker 2009-05-08 14:53:52 +0200 2942) if (ret)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2943) goto journal_error;
d6f5b0aa08078 (Frederic Weisbecker 2009-05-08 14:53:52 +0200 2944)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2945) reiserfs_update_inode_transaction(inode);
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2946) inode->i_size = pos + copied;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2947) /*
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2948) * this will just nest into our transaction. It's important
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2949) * to use mark_inode_dirty so the inode gets pushed around on
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 2950) * the dirty lists, and so that O_SYNC works as expected
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2951) */
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2952) mark_inode_dirty(inode);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2953) reiserfs_update_sd(&myth, inode);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2954) update_sd = 1;
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 2955) ret = journal_end(&myth);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2956) if (ret)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2957) goto journal_error;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2958) }
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2959) if (th) {
d6f5b0aa08078 (Frederic Weisbecker 2009-05-08 14:53:52 +0200 2960) if (!locked) {
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 2961) reiserfs_write_lock(inode->i_sb);
d6f5b0aa08078 (Frederic Weisbecker 2009-05-08 14:53:52 +0200 2962) locked = true;
d6f5b0aa08078 (Frederic Weisbecker 2009-05-08 14:53:52 +0200 2963) }
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2964) if (!update_sd)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2965) mark_inode_dirty(inode);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2966) ret = reiserfs_end_persistent_transaction(th);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2967) if (ret)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2968) goto out;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2969) }
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2970)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2971) out:
d6f5b0aa08078 (Frederic Weisbecker 2009-05-08 14:53:52 +0200 2972) if (locked)
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 2973) reiserfs_write_unlock(inode->i_sb);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2974) unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2975) put_page(page);
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2976)
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2977) if (pos + len > inode->i_size)
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2978) reiserfs_truncate_failed_write(inode);
ec8e2f7466ca3 (Jan Kara 2009-12-17 15:27:06 -0800 2979)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2980) return ret == 0 ? copied : ret;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2981)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 2982) journal_error:
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 2983) reiserfs_write_unlock(inode->i_sb);
d6f5b0aa08078 (Frederic Weisbecker 2009-05-08 14:53:52 +0200 2984) locked = false;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2985) if (th) {
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2986) if (!update_sd)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2987) reiserfs_update_sd(th, inode);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2988) ret = reiserfs_end_persistent_transaction(th);
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2989) }
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2990) goto out;
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2991) }
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2992)
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2993) int reiserfs_commit_write(struct file *f, struct page *page,
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 2994) unsigned from, unsigned to)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2995) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2996) struct inode *inode = page->mapping->host;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2997) loff_t pos = ((loff_t) page->index << PAGE_SHIFT) + to;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2998) int ret = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 2999) int update_sd = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3000) struct reiserfs_transaction_handle *th = NULL;
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3001) int depth;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3002)
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3003) depth = reiserfs_write_unlock_nested(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3004) reiserfs_wait_on_write_block(inode->i_sb);
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3005) reiserfs_write_lock_nested(inode->i_sb, depth);
8ebc423238341 (Frederic Weisbecker 2009-04-07 04:19:49 +0200 3006)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3007) if (reiserfs_transaction_running(inode->i_sb)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3008) th = current->journal_info;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3009) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3010) reiserfs_commit_page(inode, page, from, to);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3011)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3012) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3013) * generic_commit_write does this for us, but does not update the
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3014) * transaction tracking stuff when the size changes. So, we have
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3015) * to do the i_size updates here.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3016) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3017) if (pos > inode->i_size) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3018) struct reiserfs_transaction_handle myth;
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3019) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3020) * If the file have grown beyond the border where it
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3021) * can have a tail, unmark it as needing a tail
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3022) * packing
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3023) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3024) if ((have_large_tails(inode->i_sb)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3025) && inode->i_size > i_block_size(inode) * 4)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3026) || (have_small_tails(inode->i_sb)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3027) && inode->i_size > i_block_size(inode)))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3028) REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3029)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3030) ret = journal_begin(&myth, inode->i_sb, 1);
7e94277050e31 (Frederic Weisbecker 2009-08-25 03:38:12 +0200 3031) if (ret)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3032) goto journal_error;
7e94277050e31 (Frederic Weisbecker 2009-08-25 03:38:12 +0200 3033)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3034) reiserfs_update_inode_transaction(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3035) inode->i_size = pos;
9f03783ce5d85 (Chris Mason 2005-09-13 01:25:17 -0700 3036) /*
9f03783ce5d85 (Chris Mason 2005-09-13 01:25:17 -0700 3037) * this will just nest into our transaction. It's important
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3038) * to use mark_inode_dirty so the inode gets pushed around
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3039) * on the dirty lists, and so that O_SYNC works as expected
9f03783ce5d85 (Chris Mason 2005-09-13 01:25:17 -0700 3040) */
9f03783ce5d85 (Chris Mason 2005-09-13 01:25:17 -0700 3041) mark_inode_dirty(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3042) reiserfs_update_sd(&myth, inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3043) update_sd = 1;
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 3044) ret = journal_end(&myth);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3045) if (ret)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3046) goto journal_error;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3047) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3048) if (th) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3049) if (!update_sd)
9f03783ce5d85 (Chris Mason 2005-09-13 01:25:17 -0700 3050) mark_inode_dirty(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3051) ret = reiserfs_end_persistent_transaction(th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3052) if (ret)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3053) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3054) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3055)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 3056) out:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3057) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3058)
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 3059) journal_error:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3060) if (th) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3061) if (!update_sd)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3062) reiserfs_update_sd(th, inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3063) ret = reiserfs_end_persistent_transaction(th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3064) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3065)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3066) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3067) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3068)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3069) void sd_attrs_to_i_attrs(__u16 sd_attrs, struct inode *inode)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3070) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3071) if (reiserfs_attrs(inode->i_sb)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3072) if (sd_attrs & REISERFS_SYNC_FL)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3073) inode->i_flags |= S_SYNC;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3074) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3075) inode->i_flags &= ~S_SYNC;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3076) if (sd_attrs & REISERFS_IMMUTABLE_FL)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3077) inode->i_flags |= S_IMMUTABLE;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3078) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3079) inode->i_flags &= ~S_IMMUTABLE;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3080) if (sd_attrs & REISERFS_APPEND_FL)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3081) inode->i_flags |= S_APPEND;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3082) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3083) inode->i_flags &= ~S_APPEND;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3084) if (sd_attrs & REISERFS_NOATIME_FL)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3085) inode->i_flags |= S_NOATIME;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3086) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3087) inode->i_flags &= ~S_NOATIME;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3088) if (sd_attrs & REISERFS_NOTAIL_FL)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3089) REISERFS_I(inode)->i_flags |= i_nopack_mask;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3090) else
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3091) REISERFS_I(inode)->i_flags &= ~i_nopack_mask;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3092) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3093) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3094)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3095) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3096) * decide if this buffer needs to stay around for data logging or ordered
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3097) * write purposes
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3098) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3099) static int invalidatepage_can_drop(struct inode *inode, struct buffer_head *bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3100) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3101) int ret = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3102) struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3103)
d62b1b87a7d1c (Chris Mason 2006-02-01 03:06:47 -0800 3104) lock_buffer(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3105) spin_lock(&j->j_dirty_buffers_lock);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3106) if (!buffer_mapped(bh)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3107) goto free_jh;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3108) }
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3109) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3110) * the page is locked, and the only places that log a data buffer
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3111) * also lock the page.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3112) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3113) if (reiserfs_file_data_log(inode)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3114) /*
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3115) * very conservative, leave the buffer pinned if
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3116) * anyone might need it.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3117) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3118) if (buffer_journaled(bh) || buffer_journal_dirty(bh)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3119) ret = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3120) }
d62b1b87a7d1c (Chris Mason 2006-02-01 03:06:47 -0800 3121) } else if (buffer_dirty(bh)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3122) struct reiserfs_journal_list *jl;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3123) struct reiserfs_jh *jh = bh->b_private;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3124)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3125) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3126) * why is this safe?
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3127) * reiserfs_setattr updates i_size in the on disk
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3128) * stat data before allowing vmtruncate to be called.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3129) *
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3130) * If buffer was put onto the ordered list for this
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3131) * transaction, we know for sure either this transaction
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3132) * or an older one already has updated i_size on disk,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3133) * and this ordered data won't be referenced in the file
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3134) * if we crash.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3135) *
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3136) * if the buffer was put onto the ordered list for an older
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3137) * transaction, we need to leave it around
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3138) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3139) if (jh && (jl = jh->jl)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3140) && jl != SB_JOURNAL(inode->i_sb)->j_current_jl)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3141) ret = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3142) }
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 3143) free_jh:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3144) if (ret && bh->b_private) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3145) reiserfs_free_jh(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3146) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3147) spin_unlock(&j->j_dirty_buffers_lock);
d62b1b87a7d1c (Chris Mason 2006-02-01 03:06:47 -0800 3148) unlock_buffer(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3149) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3150) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3151)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3152) /* clm -- taken from fs/buffer.c:block_invalidate_page */
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 3153) static void reiserfs_invalidatepage(struct page *page, unsigned int offset,
d47992f86b307 (Lukas Czerner 2013-05-21 23:17:23 -0400 3154) unsigned int length)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3155) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3156) struct buffer_head *head, *bh, *next;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3157) struct inode *inode = page->mapping->host;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3158) unsigned int curr_off = 0;
bad54831968a8 (Lukas Czerner 2013-05-21 23:58:51 -0400 3159) unsigned int stop = offset + length;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 3160) int partial_page = (offset || length < PAGE_SIZE);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3161) int ret = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3162)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3163) BUG_ON(!PageLocked(page));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3164)
bad54831968a8 (Lukas Czerner 2013-05-21 23:58:51 -0400 3165) if (!partial_page)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3166) ClearPageChecked(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3167)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3168) if (!page_has_buffers(page))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3169) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3170)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3171) head = page_buffers(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3172) bh = head;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3173) do {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3174) unsigned int next_off = curr_off + bh->b_size;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3175) next = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3176)
bad54831968a8 (Lukas Czerner 2013-05-21 23:58:51 -0400 3177) if (next_off > stop)
bad54831968a8 (Lukas Czerner 2013-05-21 23:58:51 -0400 3178) goto out;
bad54831968a8 (Lukas Czerner 2013-05-21 23:58:51 -0400 3179)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3180) /*
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3181) * is this block fully invalidated?
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3182) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3183) if (offset <= curr_off) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3184) if (invalidatepage_can_drop(inode, bh))
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3185) reiserfs_unmap_buffer(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3186) else
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3187) ret = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3188) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3189) curr_off = next_off;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3190) bh = next;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3191) } while (bh != head);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3192)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3193) /*
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3194) * We release buffers only if the entire page is being invalidated.
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3195) * The get_block cached value has been unconditionally invalidated,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3196) * so real IO is not possible anymore.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3197) */
bad54831968a8 (Lukas Czerner 2013-05-21 23:58:51 -0400 3198) if (!partial_page && ret) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3199) ret = try_to_release_page(page, 0);
2ff28e22bdb87 (NeilBrown 2006-03-26 01:37:18 -0800 3200) /* maybe should BUG_ON(!ret); - neilb */
2ff28e22bdb87 (NeilBrown 2006-03-26 01:37:18 -0800 3201) }
cf776a7a4dafa (Jeff Mahoney 2014-04-23 10:00:41 -0400 3202) out:
2ff28e22bdb87 (NeilBrown 2006-03-26 01:37:18 -0800 3203) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3204) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3205)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3206) static int reiserfs_set_page_dirty(struct page *page)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3207) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3208) struct inode *inode = page->mapping->host;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3209) if (reiserfs_file_data_log(inode)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3210) SetPageChecked(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3211) return __set_page_dirty_nobuffers(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3212) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3213) return __set_page_dirty_buffers(page);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3214) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3215)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3216) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3217) * Returns 1 if the page's buffers were dropped. The page is locked.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3218) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3219) * Takes j_dirty_buffers_lock to protect the b_assoc_buffers list_heads
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3220) * in the buffers at page_buffers(page).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3221) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3222) * even in -o notail mode, we can't be sure an old mount without -o notail
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3223) * didn't create files with tails.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3224) */
27496a8c67bef (Al Viro 2005-10-21 03:20:48 -0400 3225) static int reiserfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3226) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3227) struct inode *inode = page->mapping->host;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3228) struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3229) struct buffer_head *head;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3230) struct buffer_head *bh;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3231) int ret = 1;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3232)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3233) WARN_ON(PageChecked(page));
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3234) spin_lock(&j->j_dirty_buffers_lock);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3235) head = page_buffers(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3236) bh = head;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3237) do {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3238) if (bh->b_private) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3239) if (!buffer_dirty(bh) && !buffer_locked(bh)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3240) reiserfs_free_jh(bh);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3241) } else {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3242) ret = 0;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3243) break;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3244) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3245) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3246) bh = bh->b_this_page;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3247) } while (bh != head);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3248) if (ret)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3249) ret = try_to_free_buffers(page);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3250) spin_unlock(&j->j_dirty_buffers_lock);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3251) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3252) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3253)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3254) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3255) * We thank Mingming Cao for helping us understand in great detail what
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3256) * to do in this section of the code.
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3257) */
c8b8e32d700fe (Christoph Hellwig 2016-04-07 08:51:58 -0700 3258) static ssize_t reiserfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3259) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3260) struct file *file = iocb->ki_filp;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3261) struct inode *inode = file->f_mapping->host;
a6cbcd4a4a85e (Al Viro 2014-03-04 22:38:00 -0500 3262) size_t count = iov_iter_count(iter);
eafdc7d190a94 (Christoph Hellwig 2010-06-04 11:29:53 +0200 3263) ssize_t ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3264)
c8b8e32d700fe (Christoph Hellwig 2016-04-07 08:51:58 -0700 3265) ret = blockdev_direct_IO(iocb, inode, iter,
31b140398ce56 (Al Viro 2014-03-05 01:33:16 -0500 3266) reiserfs_get_blocks_direct_io);
eafdc7d190a94 (Christoph Hellwig 2010-06-04 11:29:53 +0200 3267)
eafdc7d190a94 (Christoph Hellwig 2010-06-04 11:29:53 +0200 3268) /*
eafdc7d190a94 (Christoph Hellwig 2010-06-04 11:29:53 +0200 3269) * In case of error extending write may have instantiated a few
eafdc7d190a94 (Christoph Hellwig 2010-06-04 11:29:53 +0200 3270) * blocks outside i_size. Trim these off again.
eafdc7d190a94 (Christoph Hellwig 2010-06-04 11:29:53 +0200 3271) */
6f67376318abe (Omar Sandoval 2015-03-16 04:33:52 -0700 3272) if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
eafdc7d190a94 (Christoph Hellwig 2010-06-04 11:29:53 +0200 3273) loff_t isize = i_size_read(inode);
c8b8e32d700fe (Christoph Hellwig 2016-04-07 08:51:58 -0700 3274) loff_t end = iocb->ki_pos + count;
eafdc7d190a94 (Christoph Hellwig 2010-06-04 11:29:53 +0200 3275)
cfac4b47c664e (Marco Stornelli 2012-12-15 11:47:31 +0100 3276) if ((end > isize) && inode_newsize_ok(inode, isize) == 0) {
cfac4b47c664e (Marco Stornelli 2012-12-15 11:47:31 +0100 3277) truncate_setsize(inode, isize);
cfac4b47c664e (Marco Stornelli 2012-12-15 11:47:31 +0100 3278) reiserfs_vfs_truncate_file(inode);
cfac4b47c664e (Marco Stornelli 2012-12-15 11:47:31 +0100 3279) }
eafdc7d190a94 (Christoph Hellwig 2010-06-04 11:29:53 +0200 3280) }
eafdc7d190a94 (Christoph Hellwig 2010-06-04 11:29:53 +0200 3281)
eafdc7d190a94 (Christoph Hellwig 2010-06-04 11:29:53 +0200 3282) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3283) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3284)
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 3285) int reiserfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 3286) struct iattr *attr)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3287) {
2b0143b5c986b (David Howells 2015-03-17 22:25:59 +0000 3288) struct inode *inode = d_inode(dentry);
cdd6fe6e2f7eb (Jeff Layton 2007-10-18 03:05:19 -0700 3289) unsigned int ia_valid;
5fe1533fda8ae (Frederic Weisbecker 2010-01-04 22:04:01 +0100 3290) int error;
cdd6fe6e2f7eb (Jeff Layton 2007-10-18 03:05:19 -0700 3291)
2f221d6f7b881 (Christian Brauner 2021-01-21 14:19:26 +0100 3292) error = setattr_prepare(&init_user_ns, dentry, attr);
db78b877f7744 (Christoph Hellwig 2010-06-04 11:30:03 +0200 3293) if (error)
db78b877f7744 (Christoph Hellwig 2010-06-04 11:30:03 +0200 3294) return error;
db78b877f7744 (Christoph Hellwig 2010-06-04 11:30:03 +0200 3295)
cdd6fe6e2f7eb (Jeff Layton 2007-10-18 03:05:19 -0700 3296) /* must be turned off for recursive notify_change calls */
cdd6fe6e2f7eb (Jeff Layton 2007-10-18 03:05:19 -0700 3297) ia_valid = attr->ia_valid &= ~(ATTR_KILL_SUID|ATTR_KILL_SGID);
cdd6fe6e2f7eb (Jeff Layton 2007-10-18 03:05:19 -0700 3298)
2e6c97ea4ce6a (Jan Kara 2015-07-14 13:51:55 +0200 3299) if (is_quota_modification(inode, attr)) {
2e6c97ea4ce6a (Jan Kara 2015-07-14 13:51:55 +0200 3300) error = dquot_initialize(inode);
2e6c97ea4ce6a (Jan Kara 2015-07-14 13:51:55 +0200 3301) if (error)
2e6c97ea4ce6a (Jan Kara 2015-07-14 13:51:55 +0200 3302) return error;
2e6c97ea4ce6a (Jan Kara 2015-07-14 13:51:55 +0200 3303) }
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3304) reiserfs_write_lock(inode->i_sb);
12755627bdcdd (Dmitry Monakhov 2010-04-08 22:04:20 +0400 3305) if (attr->ia_valid & ATTR_SIZE) {
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3306) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3307) * version 2 items will be caught by the s_maxbytes check
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3308) * done for us in vmtruncate
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3309) */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3310) if (get_inode_item_key_version(inode) == KEY_FORMAT_3_5 &&
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3311) attr->ia_size > MAX_NON_LFS) {
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3312) reiserfs_write_unlock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3313) error = -EFBIG;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3314) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3315) }
562c72aa57c36 (Christoph Hellwig 2011-06-24 14:29:45 -0400 3316)
562c72aa57c36 (Christoph Hellwig 2011-06-24 14:29:45 -0400 3317) inode_dio_wait(inode);
562c72aa57c36 (Christoph Hellwig 2011-06-24 14:29:45 -0400 3318)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3319) /* fill in hole pointers in the expanding truncate case. */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3320) if (attr->ia_size > inode->i_size) {
f7557e8f7ff78 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 3321) error = generic_cont_expand_simple(inode, attr->ia_size);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3322) if (REISERFS_I(inode)->i_prealloc_count > 0) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3323) int err;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3324) struct reiserfs_transaction_handle th;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3325) /* we're changing at most 2 bitmaps, inode + super */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3326) err = journal_begin(&th, inode->i_sb, 4);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3327) if (!err) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3328) reiserfs_discard_prealloc(&th, inode);
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 3329) err = journal_end(&th);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3330) }
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3331) if (err)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3332) error = err;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3333) }
4c05141df57f4 (Jeff Mahoney 2013-08-08 17:27:19 -0400 3334) if (error) {
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3335) reiserfs_write_unlock(inode->i_sb);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3336) goto out;
4c05141df57f4 (Jeff Mahoney 2013-08-08 17:27:19 -0400 3337) }
dd535a5965cef (Vladimir Saveliev 2006-07-01 04:36:32 -0700 3338) /*
dd535a5965cef (Vladimir Saveliev 2006-07-01 04:36:32 -0700 3339) * file size is changed, ctime and mtime are
dd535a5965cef (Vladimir Saveliev 2006-07-01 04:36:32 -0700 3340) * to be updated
dd535a5965cef (Vladimir Saveliev 2006-07-01 04:36:32 -0700 3341) */
dd535a5965cef (Vladimir Saveliev 2006-07-01 04:36:32 -0700 3342) attr->ia_valid |= (ATTR_MTIME | ATTR_CTIME);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3343) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3344) }
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3345) reiserfs_write_unlock(inode->i_sb);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3346)
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 3347) if ((((attr->ia_valid & ATTR_UID) && (from_kuid(&init_user_ns, attr->ia_uid) & ~0xffff)) ||
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 3348) ((attr->ia_valid & ATTR_GID) && (from_kgid(&init_user_ns, attr->ia_gid) & ~0xffff))) &&
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3349) (get_inode_sd_version(inode) == STAT_DATA_V1)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3350) /* stat data of format v3.5 has 16 bit uid and gid */
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3351) error = -EINVAL;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3352) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3353) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3354)
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 3355) if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
df814654f3643 (Eric W. Biederman 2012-02-07 16:30:06 -0800 3356) (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3357) struct reiserfs_transaction_handle th;
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3358) int jbegin_count =
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3359) 2 *
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3360) (REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb) +
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3361) REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb)) +
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3362) 2;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3363)
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3364) error = reiserfs_chown_xattrs(inode, attr);
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3365)
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3366) if (error)
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3367) return error;
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3368)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3369) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3370) * (user+group)*(old+new) structure - we count quota
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3371) * info and , inode write (sb, inode)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3372) */
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3373) reiserfs_write_lock(inode->i_sb);
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3374) error = journal_begin(&th, inode->i_sb, jbegin_count);
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3375) reiserfs_write_unlock(inode->i_sb);
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3376) if (error)
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3377) goto out;
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3378) error = dquot_transfer(inode, attr);
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3379) reiserfs_write_lock(inode->i_sb);
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3380) if (error) {
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 3381) journal_end(&th);
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3382) reiserfs_write_unlock(inode->i_sb);
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3383) goto out;
108d3943c021f (Frederic Weisbecker 2010-01-05 00:15:38 +0100 3384) }
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3385)
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3386) /*
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3387) * Update corresponding info in inode so that everything
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3388) * is in one transaction
098297b27d23a (Jeff Mahoney 2014-04-23 10:00:36 -0400 3389) */
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3390) if (attr->ia_valid & ATTR_UID)
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3391) inode->i_uid = attr->ia_uid;
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3392) if (attr->ia_valid & ATTR_GID)
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3393) inode->i_gid = attr->ia_gid;
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3394) mark_inode_dirty(inode);
58d854265c4c7 (Jeff Mahoney 2014-04-23 10:00:38 -0400 3395) error = journal_end(&th);
278f6679f454b (Jeff Mahoney 2013-08-08 17:34:46 -0400 3396) reiserfs_write_unlock(inode->i_sb);
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3397) if (error)
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3398) goto out;
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3399) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3400)
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3401) if ((attr->ia_valid & ATTR_SIZE) &&
cfac4b47c664e (Marco Stornelli 2012-12-15 11:47:31 +0100 3402) attr->ia_size != i_size_read(inode)) {
cfac4b47c664e (Marco Stornelli 2012-12-15 11:47:31 +0100 3403) error = inode_newsize_ok(inode, attr->ia_size);
cfac4b47c664e (Marco Stornelli 2012-12-15 11:47:31 +0100 3404) if (!error) {
22e7478ddbcb6 (Jeff Mahoney 2014-05-21 13:28:07 -0400 3405) /*
22e7478ddbcb6 (Jeff Mahoney 2014-05-21 13:28:07 -0400 3406) * Could race against reiserfs_file_release
22e7478ddbcb6 (Jeff Mahoney 2014-05-21 13:28:07 -0400 3407) * if called from NFS, so take tailpack mutex.
22e7478ddbcb6 (Jeff Mahoney 2014-05-21 13:28:07 -0400 3408) */
22e7478ddbcb6 (Jeff Mahoney 2014-05-21 13:28:07 -0400 3409) mutex_lock(&REISERFS_I(inode)->tailpack);
cfac4b47c664e (Marco Stornelli 2012-12-15 11:47:31 +0100 3410) truncate_setsize(inode, attr->ia_size);
22e7478ddbcb6 (Jeff Mahoney 2014-05-21 13:28:07 -0400 3411) reiserfs_truncate_file(inode, 1);
22e7478ddbcb6 (Jeff Mahoney 2014-05-21 13:28:07 -0400 3412) mutex_unlock(&REISERFS_I(inode)->tailpack);
cfac4b47c664e (Marco Stornelli 2012-12-15 11:47:31 +0100 3413) }
cfac4b47c664e (Marco Stornelli 2012-12-15 11:47:31 +0100 3414) }
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3415)
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3416) if (!error) {
2f221d6f7b881 (Christian Brauner 2021-01-21 14:19:26 +0100 3417) setattr_copy(&init_user_ns, inode, attr);
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 3418) mark_inode_dirty(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3419) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3420)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3421) if (!error && reiserfs_posixacl(inode->i_sb)) {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3422) if (attr->ia_valid & ATTR_MODE)
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3423) error = reiserfs_acl_chmod(inode);
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3424) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3425)
4c05141df57f4 (Jeff Mahoney 2013-08-08 17:27:19 -0400 3426) out:
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3427) return error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3428) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3429)
f5e54d6e53a20 (Christoph Hellwig 2006-06-28 04:26:44 -0700 3430) const struct address_space_operations reiserfs_address_space_operations = {
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3431) .writepage = reiserfs_writepage,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3432) .readpage = reiserfs_readpage,
d4388340ae0bc (Matthew Wilcox (Oracle) 2020-06-01 21:47:02 -0700 3433) .readahead = reiserfs_readahead,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3434) .releasepage = reiserfs_releasepage,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3435) .invalidatepage = reiserfs_invalidatepage,
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 3436) .write_begin = reiserfs_write_begin,
ba9d8cec6c716 (Vladimir Saveliev 2007-10-16 01:25:14 -0700 3437) .write_end = reiserfs_write_end,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3438) .bmap = reiserfs_aop_bmap,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3439) .direct_IO = reiserfs_direct_IO,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3440) .set_page_dirty = reiserfs_set_page_dirty,
bd4c625c061c2 (Linus Torvalds 2005-07-12 20:21:28 -0700 3441) };