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