blob: caa758377d66b8ba5530d029635d8cdfa98048ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#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 Hellwiga5694252007-07-17 04:04:28 -070010#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/smp_lock.h>
12#include <linux/pagemap.h>
13#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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 Savelievba9d8ce2007-10-16 01:25:14 -070021#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -070023int reiserfs_commit_write(struct file *f, struct page *page,
24 unsigned from, unsigned to);
25int reiserfs_prepare_write(struct file *f, struct page *page,
26 unsigned from, unsigned to);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Al Viro845a2cc2010-06-07 11:37:37 -040028void reiserfs_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070030 /* 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 Weisbeckercb1c2e52009-12-13 23:32:06 +010035 int depth;
Jeff Mahoney24996042005-12-14 14:38:05 -050036 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Al Viro845a2cc2010-06-07 11:37:37 -040038 if (!inode->i_nlink && !is_bad_inode(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -050039 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -050040
Mark Fashehfef26652005-09-09 13:01:31 -070041 truncate_inode_pages(&inode->i_data, 0);
Al Viro845a2cc2010-06-07 11:37:37 -040042 if (inode->i_nlink)
43 goto no_delete;
Mark Fashehfef26652005-09-09 13:01:31 -070044
Frederic Weisbeckercb1c2e52009-12-13 23:32:06 +010045 depth = reiserfs_write_lock_once(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070047 /* 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 Torvaldsbd4c6252005-07-12 20:21:28 -070049 reiserfs_delete_xattrs(inode);
50
Alexander Zarochentsevb0b33de2006-08-05 12:14:01 -070051 if (journal_begin(&th, inode->i_sb, jbegin_count))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070052 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070053 reiserfs_update_inode_transaction(inode);
54
Jeff Mahoneyeb35c212008-07-08 14:37:06 -040055 reiserfs_discard_prealloc(&th, inode);
56
Jeff Mahoney24996042005-12-14 14:38:05 -050057 err = reiserfs_delete_object(&th, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070058
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 Mahoney24996042005-12-14 14:38:05 -050062 if (!err)
Christoph Hellwig63936dd2010-03-03 09:05:01 -050063 dquot_free_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070064
Alexander Zarochentsevb0b33de2006-08-05 12:14:01 -070065 if (journal_end(&th, inode->i_sb, jbegin_count))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070066 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070067
Jeff Mahoney24996042005-12-14 14:38:05 -050068 /* check return value from reiserfs_delete_object after
69 * ending the transaction
70 */
71 if (err)
72 goto out;
73
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070074 /* 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 Torvalds1da177e2005-04-16 15:20:36 -070080 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070081 out:
Al Viro845a2cc2010-06-07 11:37:37 -040082 end_writeback(inode); /* note this must go after the journal_end to prevent deadlock */
83 dquot_drop(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084 inode->i_blocks = 0;
Frederic Weisbeckercb1c2e52009-12-13 23:32:06 +010085 reiserfs_write_unlock_once(inode->i_sb, depth);
Sergey Senozhatskyf4ae2fa2010-08-11 14:07:01 +030086 return;
Al Viro845a2cc2010-06-07 11:37:37 -040087
88no_delete:
89 end_writeback(inode);
90 dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093static void _make_cpu_key(struct cpu_key *key, int version, __u32 dirid,
94 __u32 objectid, loff_t offset, int type, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070096 key->version = version;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070098 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 Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/* 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 Torvaldsbd4c6252005-07-12 20:21:28 -0700107void make_cpu_key(struct cpu_key *key, struct inode *inode, loff_t offset,
108 int type, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700110 _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 Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116//
117// when key is 0, do not set version and short key
118//
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700119inline 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 Torvalds1da177e2005-04-16 15:20:36 -0700123{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700124 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 Torvalds1da177e2005-04-16 15:20:36 -0700137}
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 Torvaldsbd4c6252005-07-12 20:21:28 -0700171static inline void fix_tail_page_for_writing(struct page *page)
172{
173 struct buffer_head *head, *next, *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700175 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 Torvalds1da177e2005-04-16 15:20:36 -0700186}
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 Torvaldsbd4c6252005-07-12 20:21:28 -0700190static inline int allocation_needed(int retval, b_blocknr_t allocated,
191 struct item_head *ih,
192 __le32 * item, int pos_in_item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194 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 Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700202static inline int indirect_item_found(int retval, struct item_head *ih)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 return (retval == POSITION_FOUND) && is_indirect_le_ih(ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700207static inline void set_block_dev_mapped(struct buffer_head *bh,
208 b_blocknr_t block, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 map_bh(bh, inode->i_sb, block);
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213//
214// files which were created in the earlier version can not be longer,
215// than 2 gb
216//
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700217static int file_capable(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700219 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 Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Adrian Bunkdeba0f42007-10-16 23:26:03 -0700226static int restart_transaction(struct reiserfs_transaction_handle *th,
227 struct inode *inode, struct treepath *path)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700228{
229 struct super_block *s = th->t_super;
230 int len = th->t_blocks_allocated;
231 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700233 BUG_ON(!th->t_trans_id);
234 BUG_ON(!th->t_refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Suzuki K P87b41262006-12-06 20:36:10 -0800236 pathrelse(path);
237
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700238 /* we cannot restart while nested */
239 if (th->t_refcount > 1) {
240 return 0;
241 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700242 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 Torvalds1da177e2005-04-16 15:20:36 -0700250}
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 Mahoney3ee16672007-10-18 23:39:25 -0700260static int _get_block_create_0(struct inode *inode, sector_t block,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700261 struct buffer_head *bh_result, int args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700263 INITIALIZE_PATH(path);
264 struct cpu_key key;
265 struct buffer_head *bh;
266 struct item_head *ih, tmp_ih;
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700267 b_blocknr_t blocknr;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700268 char *p = NULL;
269 int chars;
270 int ret;
271 int result;
272 int done = 0;
273 unsigned long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700275 // 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 Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700280 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 Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700295 //
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 Torvalds1da177e2005-04-16 15:20:36 -0700300
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700301 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700319
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700320 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 Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700335 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700349 set_buffer_uptodate(bh_result);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700350 goto finished;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700352 // read file tail into part of page
353 offset = (cpu_key_k_offset(&key) - 1) & (PAGE_CACHE_SIZE - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700354 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 Weisbecker27b3a5c2009-10-14 23:34:31 +0200361 if (!p)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700362 p = (char *)kmap(bh_result->b_page);
Frederic Weisbecker27b3a5c2009-10-14 23:34:31 +0200363
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700364 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 Mahoney0222e652009-03-30 14:02:44 -0400372 ** correct, and when direct item padding results in a few
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700373 ** 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 Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700387 if (done)
388 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700390 p += chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700392 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 Torvalds1da177e2005-04-16 15:20:36 -0700398
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399 // 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 Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700409 flush_dcache_page(bh_result->b_page);
410 kunmap(bh_result->b_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700412 finished:
413 pathrelse(&path);
Qu Fuping6283d582005-06-25 14:55:44 -0700414
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700415 if (result == IO_ERROR)
416 return -EIO;
Qu Fuping6283d582005-06-25 14:55:44 -0700417
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700418 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426// this is called to create file map. So, _get_block_create_0 will not
427// read direct item
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700428static int reiserfs_bmap(struct inode *inode, sector_t block,
429 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700431 if (!file_capable(inode, block))
432 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700434 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 Torvalds1da177e2005-04-16 15:20:36 -0700439}
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 Mahoney0222e652009-03-30 14:02:44 -0400447**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448** 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 Mahoney0222e652009-03-30 14:02:44 -0400454** 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 Torvalds1da177e2005-04-16 15:20:36 -0700456** don't use this function.
457*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700458static 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 Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465/* This is special helper for reiserfs_get_block in case we are executing
466 direct_IO request. */
467static int reiserfs_get_blocks_direct_io(struct inode *inode,
468 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct buffer_head *bh_result,
470 int create)
471{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700472 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700474 bh_result->b_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700476 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700480
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700481 ret = reiserfs_get_block(inode, iblock, bh_result,
482 create | GET_BLOCK_NO_DANGLE);
483 if (ret)
484 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700486 /* 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 Weisbecker8ebc4232009-04-07 04:19:49 +0200498
499 reiserfs_write_lock(inode->i_sb);
500
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700501 err = reiserfs_commit_for_inode(inode);
502 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200503
504 reiserfs_write_unlock(inode->i_sb);
505
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700506 if (err < 0)
507 ret = err;
508 }
509 out:
510 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/*
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 Torvaldsbd4c6252005-07-12 20:21:28 -0700523static 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 Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700534 if ((tail_offset & (bh_result->b_size - 1)) != 1)
535 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700537 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700541 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 Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700555 /* 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 Sorensen1b1dcc12006-01-09 15:59:24 -0800557 ** must first take i_mutex.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700558 **
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 Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700569 /* tail conversion might change the data in the page */
570 flush_dcache_page(tail_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700572 retval = reiserfs_commit_write(NULL, tail_page, tail_start, tail_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700574 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 Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583static inline int _allocate_block(struct reiserfs_transaction_handle *th,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700584 sector_t block,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700585 struct inode *inode,
586 b_blocknr_t * allocated_block_nr,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800587 struct treepath *path, int flags)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700588{
589 BUG_ON(!th->t_trans_id);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#ifdef REISERFS_PREALLOCATE
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800592 if (!(flags & GET_BLOCK_NO_IMUX)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700593 return reiserfs_new_unf_blocknrs2(th, inode, allocated_block_nr,
594 path, block);
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700597 return reiserfs_new_unf_blocknrs(th, inode, allocated_block_nr, path,
598 block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700601int reiserfs_get_block(struct inode *inode, sector_t block,
602 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700604 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 Weisbecker26931302009-05-07 23:48:44 +0200614 int lock_depth;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700615 struct reiserfs_transaction_handle *th = NULL;
Jeff Mahoney0222e652009-03-30 14:02:44 -0400616 /* space reserved in transaction batch:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617 . 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 Torvalds1da177e2005-04-16 15:20:36 -0700629
Frederic Weisbecker26931302009-05-07 23:48:44 +0200630 lock_depth = reiserfs_write_lock_once(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700631 version = get_inode_item_key_version(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700633 if (!file_capable(inode, block)) {
Frederic Weisbecker26931302009-05-07 23:48:44 +0200634 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700635 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700638 /* 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 Weisbecker26931302009-05-07 23:48:44 +0200646 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700647 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700649 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700656
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700657 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700677 research:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700679 retval = search_for_position_by_key(inode->i_sb, &key, &path);
680 if (retval == IO_ERROR) {
681 retval = -EIO;
682 goto failure;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700685 bh = get_last_bh(&path);
686 ih = get_ih(&path);
687 item = get_item(&path);
688 pos_in_item = path.pos_in_item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700690 fs_gen = get_generation(inode->i_sb);
691 copy_item_head(&tmp_ih, ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700693 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 Torvalds1da177e2005-04-16 15:20:36 -0700700
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700701 repeat =
702 _allocate_block(th, block, inode, &allocated_block_nr,
703 &path, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700705 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 Torvalds1da177e2005-04-16 15:20:36 -0700717
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700718 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 Torvalds1da177e2005-04-16 15:20:36 -0700725 goto failure;
726 }
727
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700728 if (fs_changed(fs_gen, inode->i_sb)
729 && item_moved(&tmp_ih, &path)) {
730 goto research;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700734 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 Torvalds1da177e2005-04-16 15:20:36 -0700763
Frederic Weisbecker26931302009-05-07 23:48:44 +0200764 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700765
766 /* the item was found, so new blocks were not added to the file
Jeff Mahoney0222e652009-03-30 14:02:44 -0400767 ** there is no need to make sure the inode is updated with this
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700768 ** transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700773 if (!th) {
774 pathrelse(&path);
775 goto start_trans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700778 /* 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 Mahoney0030b642009-03-30 14:02:28 -0400854 reiserfs_error(inode->i_sb,
855 "clm-6004",
856 "convert tail failed "
857 "inode %lu, error %d",
858 inode->i_ino,
859 retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700860 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. Savelievc499ec22006-03-02 02:54:39 -0800937 RFALSE(cpu_key_k_offset(&tmp_key) > cpu_key_k_offset(&key),
938 "green-805: invalid offset");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700939 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 Torvaldsbd4c6252005-07-12 20:21:28 -0700944
945 if (blocks_needed == 1) {
946 un = &unf_single;
947 } else {
Frederic Weisbecker1d2c6cf2009-11-20 04:11:30 +0100948 un = kzalloc(min(blocks_needed, max_to_insert) * UNFM_P_SIZE, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700949 if (!un) {
950 un = &unf_single;
951 blocks_needed = 1;
952 max_to_insert = 0;
Yan Burman01afb212006-12-06 20:39:01 -0800953 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700954 }
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 Mahoney0222e652009-03-30 14:02:44 -0400999 ** too big or too full. Update the inode so things are
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001000 ** 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 Weisbecker8ebc4232009-04-07 04:19:49 +02001010 /*
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 Torvaldsbd4c6252005-07-12 20:21:28 -07001014 */
Frederic Weisbecker26931302009-05-07 23:48:44 +02001015 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 Torvaldsbd4c6252005-07-12 20:21:28 -07001020
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 Mahoney45b03d52009-03-30 14:02:21 -04001027 reiserfs_warning(inode->i_sb, "vs-825",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001028 "%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 Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Frederic Weisbecker26931302009-05-07 23:48:44 +02001054 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001055 reiserfs_check_path(&path);
1056 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059static int
1060reiserfs_readpages(struct file *file, struct address_space *mapping,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001061 struct list_head *pages, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001063 return mpage_readpages(mapping, pages, nr_pages, reiserfs_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
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 */
1069static int real_space_diff(struct inode *inode, int sd_size)
1070{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001071 int bytes;
1072 loff_t blocksize = inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001074 if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode))
1075 return sd_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001077 /* 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 Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092static inline loff_t to_real_used_space(struct inode *inode, ulong blocks,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001093 int sd_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001095 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 Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
1103/* Compute number of blocks used by file in ReiserFS counting */
1104static inline ulong to_fake_used_blocks(struct inode *inode, int sd_size)
1105{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001106 loff_t bytes = inode_get_bytes(inode);
1107 loff_t real_space = real_space_diff(inode, sd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001109 /* 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 Torvalds1da177e2005-04-16 15:20:36 -07001113
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001114 /* 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 Torvalds1da177e2005-04-16 15:20:36 -07001121}
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" Sipekfec6d052006-12-08 02:36:32 -08001131static void init_inode(struct inode *inode, struct treepath *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001133 struct buffer_head *bh;
1134 struct item_head *ih;
1135 __u32 rdev;
1136 //int version = ITEM_VERSION_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001138 bh = PATH_PLAST_BUFFER(path);
1139 ih = PATH_PITEM_HEAD(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001141 copy_key(INODE_PKEY(inode), &(ih->ih_key));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001143 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 Dobriyan068fbb32006-09-29 01:59:58 -07001149 reiserfs_init_xattr_rwsem(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001151 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 Torvalds1da177e2005-04-16 15:20:36 -07001155
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001156 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 Torvalds1da177e2005-04-16 15:20:36 -07001169
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001170 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 Torvalds1da177e2005-04-16 15:20:36 -07001182
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001183 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önig421f91d2010-06-11 12:17:00 +02001231 /* read persistent inode attributes from sd and initialise
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001232 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 Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001237 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 Torvalds1da177e2005-04-16 15:20:36 -07001252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255// update new stat data with inode fields
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001256static void inode2sd(void *sd, struct inode *inode, loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001258 struct stat_data *sd_v2 = (struct stat_data *)sd;
1259 __u16 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001261 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 Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279// used to copy inode's fields to old stat data
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001280static void inode2sd_v1(void *sd, struct inode *inode, loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001282 struct stat_data_v1 *sd_v1 = (struct stat_data_v1 *)sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001284 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 Torvalds1da177e2005-04-16 15:20:36 -07001292
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001293 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 Torvalds1da177e2005-04-16 15:20:36 -07001297
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001298 // 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 Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303/* NOTE, you must prepare the buffer head before sending it here,
1304** and then log it after the call
1305*/
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -08001306static void update_stat_data(struct treepath *path, struct inode *inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001307 loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001309 struct buffer_head *bh;
1310 struct item_head *ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001312 bh = PATH_PLAST_BUFFER(path);
1313 ih = PATH_PITEM_HEAD(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001315 if (!is_statdata_le_ih(ih))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001316 reiserfs_panic(inode->i_sb, "vs-13065", "key %k, found item %h",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001317 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 Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001329void reiserfs_update_sd_size(struct reiserfs_transaction_handle *th,
1330 struct inode *inode, loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001332 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 Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001339 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001341 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 Mahoney0030b642009-03-30 14:02:28 -04001348 reiserfs_error(inode->i_sb, "vs-13050",
1349 "i/o failure occurred trying to "
1350 "update %K stat data", &key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001351 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 Mahoney45b03d52009-03-30 14:02:21 -04001360 reiserfs_warning(inode->i_sb, "vs-13060",
1361 "stat data of object %k (nlink == %d) "
1362 "not found (pos %d)",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001363 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 Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001385 update_stat_data(&path, inode, size);
1386 journal_mark_dirty(th, th->t_super, bh);
1387 pathrelse(&path);
1388 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
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 Torvaldsbd4c6252005-07-12 20:21:28 -07001397static void reiserfs_make_bad_inode(struct inode *inode)
1398{
1399 memset(INODE_PKEY(inode), 0, KEY_SIZE);
1400 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403//
1404// initially this function was derived from minix or ext2's analog and
1405// evolved as the prototype did
1406//
1407
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001408int reiserfs_init_locked_inode(struct inode *inode, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001410 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 Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416/* looks for stat data in the tree, and fills up the fields of in-core
1417 inode stat data fields */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001418void reiserfs_read_locked_inode(struct inode *inode,
1419 struct reiserfs_iget_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001421 INITIALIZE_PATH(path_to_sd);
1422 struct cpu_key key;
1423 unsigned long dirino;
1424 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001426 dirino = args->dirid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001428 /* 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 Torvalds1da177e2005-04-16 15:20:36 -07001435
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001436 /* look for the object's stat data */
1437 retval = search_item(inode->i_sb, &key, &path_to_sd);
1438 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -04001439 reiserfs_error(inode->i_sb, "vs-13070",
1440 "i/o failure occurred trying to find "
1441 "stat data of %K", &key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001442 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 Torvalds1da177e2005-04-16 15:20:36 -07001452
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001453 init_inode(inode, &path_to_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001455 /* 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 Mahoney0222e652009-03-30 14:02:44 -04001460 Gramoulle <philippe.gramoulle@mmania.com> reproduced it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001462 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 Mahoney45b03d52009-03-30 14:02:21 -04001471 reiserfs_warning(inode->i_sb, "vs-13075",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001472 "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 Torvalds1da177e2005-04-16 15:20:36 -07001479
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 Torvaldsbd4c6252005-07-12 20:21:28 -07001494int reiserfs_find_actor(struct inode *inode, void *opaque)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001496 struct reiserfs_iget_args *args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001498 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 Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001504struct inode *reiserfs_iget(struct super_block *s, const struct cpu_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001506 struct inode *inode;
1507 struct reiserfs_iget_args args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001509 args.objectid = key->on_disk_key.k_objectid;
1510 args.dirid = key->on_disk_key.k_dir_id;
Frederic Weisbecker175359f2010-02-11 13:13:10 +01001511 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001512 inode = iget5_locked(s, key->on_disk_key.k_objectid,
1513 reiserfs_find_actor, reiserfs_init_locked_inode,
1514 (void *)(&args));
Frederic Weisbecker175359f2010-02-11 13:13:10 +01001515 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001516 if (!inode)
1517 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001519 if (inode->i_state & I_NEW) {
1520 reiserfs_read_locked_inode(inode, &args);
1521 unlock_new_inode(inode);
1522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001524 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 Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001532static struct dentry *reiserfs_get_dentry(struct super_block *sb,
1533 u32 objectid, u32 dir_id, u32 generation)
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001536 struct cpu_key key;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001537 struct inode *inode;
1538
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001539 key.on_disk_key.k_objectid = objectid;
1540 key.on_disk_key.k_dir_id = dir_id;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001541 reiserfs_write_lock(sb);
1542 inode = reiserfs_iget(sb, &key);
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001543 if (inode && !IS_ERR(inode) && generation != 0 &&
1544 generation != inode->i_generation) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001545 iput(inode);
1546 inode = NULL;
1547 }
1548 reiserfs_write_unlock(sb);
Christoph Hellwig44003722008-08-11 15:49:04 +02001549
1550 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001553struct dentry *reiserfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1554 int fh_len, int fh_type)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001555{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001556 /* 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 Hellwigbe55caf2007-10-21 16:42:13 -07001568 if (fh_type > fh_len) {
1569 if (fh_type != 6 || fh_len != 5)
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001570 reiserfs_warning(sb, "reiserfs-13077",
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001571 "nfsd/reiserfs, fhtype=%d, len=%d - odd",
1572 fh_type, fh_len);
1573 fh_type = 5;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001576 return reiserfs_get_dentry(sb, fid->raw[0], fid->raw[1],
1577 (fh_type == 3 || fh_type >= 5) ? fid->raw[2] : 0);
1578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001580struct 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 Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001592int 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 Torvalds1da177e2005-04-16 15:20:36 -07001597
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001598 if (maxlen < 3)
1599 return 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001601 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 Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622/* 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 Hellwiga9185b42010-03-05 09:21:37 +01001629int reiserfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001630{
1631 struct reiserfs_transaction_handle th;
1632 int jbegin_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001634 if (inode->i_sb->s_flags & MS_RDONLY)
1635 return -EROFS;
1636 /* memory pressure can sometimes initiate write_inode calls with sync == 1,
Jeff Mahoney0222e652009-03-30 14:02:44 -04001637 ** these cases are just when the system needs ram, not when the
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001638 ** inode needs to reach disk for safety, and they can safely be
1639 ** ignored because the altered inode has already been logged.
1640 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001641 if (wbc->sync_mode == WB_SYNC_ALL && !(current->flags & PF_MEMALLOC)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001642 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 Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
1652/* stat data of new object is inserted already, this inserts the item
1653 containing "." and ".." entries */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001654static int reiserfs_new_directory(struct reiserfs_transaction_handle *th,
1655 struct inode *inode,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -08001656 struct item_head *ih, struct treepath *path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001657 struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001659 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 Torvalds1da177e2005-04-16 15:20:36 -07001664
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001665 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001667 _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 Mahoney0030b642009-03-30 14:02:28 -04001695 reiserfs_error(sb, "vs-13080",
1696 "i/o failure occurred creating new directory");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001697 return -EIO;
1698 }
1699 if (retval == ITEM_FOUND) {
1700 pathrelse(path);
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001701 reiserfs_warning(sb, "vs-13070",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001702 "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 Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711/* stat data of object has been inserted, this inserts the item
1712 containing the body of symlink */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001713static int reiserfs_new_symlink(struct reiserfs_transaction_handle *th, struct inode *inode, /* Inode of symlink */
1714 struct item_head *ih,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -08001715 struct treepath *path, const char *symname,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001716 int item_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001718 struct super_block *sb = th->t_super;
1719 struct cpu_key key;
1720 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001722 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001724 _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 Torvalds1da177e2005-04-16 15:20:36 -07001728
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001729 make_le_item_head(ih, NULL, KEY_FORMAT_3_5, 1, TYPE_DIRECT, item_len,
1730 0 /*free_space */ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001732 /* look for place in the tree for new item */
1733 retval = search_item(sb, &key, path);
1734 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -04001735 reiserfs_error(sb, "vs-13080",
1736 "i/o failure occurred creating new symlink");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001737 return -EIO;
1738 }
1739 if (retval == ITEM_FOUND) {
1740 pathrelse(path);
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001741 reiserfs_warning(sb, "vs-13080",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001742 "object with this key exists (%k)",
1743 &(ih->ih_key));
1744 return -EEXIST;
1745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001747 /* insert item, that is body of symlink */
1748 return reiserfs_insert_item(th, path, &key, ih, inode, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751/* 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 Mahoney0222e652009-03-30 14:02:44 -04001754 object is symlink) or nothing (if new object is regular file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
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 Torvaldsbd4c6252005-07-12 20:21:28 -07001760int reiserfs_new_inode(struct reiserfs_transaction_handle *th,
1761 struct inode *dir, int mode, const char *symname,
Jeff Mahoney0222e652009-03-30 14:02:44 -04001762 /* 0 for regular, EMTRY_DIR_SIZE for dirs,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001763 strlen (symname) for symlinks) */
1764 loff_t i_size, struct dentry *dentry,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001765 struct inode *inode,
1766 struct reiserfs_security_handle *security)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001768 struct super_block *sb;
Al Viroc1eaa262008-12-30 02:03:58 -05001769 struct reiserfs_iget_args args;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001770 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 Torvalds1da177e2005-04-16 15:20:36 -07001776
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001777 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Christoph Hellwig871a2932010-03-03 09:05:07 -05001779 dquot_initialize(inode);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001780 err = dquot_alloc_inode(inode);
1781 if (err)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001782 goto out_end_trans;
Eric Sesterhenn585b7742006-10-04 02:15:30 -07001783 if (!dir->i_nlink) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001784 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 Viroc1eaa262008-12-30 02:03:58 -05001797 args.objectid = inode->i_ino = le32_to_cpu(ih.ih_key.k_objectid);
Al Viro2f1169e2009-01-02 08:16:51 -05001798 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 Viroc1eaa262008-12-30 02:03:58 -05001804 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 Torvaldsbd4c6252005-07-12 20:21:28 -07001811 if (old_format_only(sb))
Jeff Mahoney0222e652009-03-30 14:02:44 -04001812 /* not a perfect generation count, as object ids can be reused, but
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001813 ** 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 Torvalds1da177e2005-04-16 15:20:36 -07001824#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001826 /* 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 Dobriyan068fbb32006-09-29 01:59:58 -07001851 reiserfs_init_xattr_rwsem(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001852
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001853 /* 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 Torvaldsbd4c6252005-07-12 20:21:28 -07001880 // 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 Torvaldsbd4c6252005-07-12 20:21:28 -07001883 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 Torvalds1da177e2005-04-16 15:20:36 -07001893#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001894 if (REISERFS_I(dir)->new_packing_locality)
1895 th->displace_new_blocks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001897 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 Torvalds1da177e2005-04-16 15:20:36 -07001914
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001915 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 Torvalds1da177e2005-04-16 15:20:36 -07001929
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001930 if (reiserfs_posixacl(inode->i_sb)) {
Jeff Mahoney0ab26212009-03-30 14:02:39 -04001931 retval = reiserfs_inherit_default_acl(th, dir, dentry, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001932 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 Mahoney45b03d52009-03-30 14:02:21 -04001939 reiserfs_warning(inode->i_sb, "jdm-13090",
1940 "ACLs aren't enabled in the fs, "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001941 "but vfs thinks they are!");
Jeff Mahoney6dfede62009-03-30 14:02:32 -04001942 } else if (IS_PRIVATE(dir))
1943 inode->i_flags |= S_PRIVATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001945 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 Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001958 reiserfs_update_sd(th, inode);
1959 reiserfs_check_path(&path_to_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001961 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
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 Torvaldsbd4c6252005-07-12 20:21:28 -07001967 out_bad_inode:
1968 /* Invalidate the object, nothing was inserted yet */
1969 INODE_PKEY(inode)->k_objectid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001971 /* Quota change must be inside a transaction for journaling */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001972 dquot_free_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001974 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 Hellwig9f754752010-03-03 09:05:05 -05001977 dquot_drop(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001978 inode->i_flags |= S_NOQUOTA;
1979 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001981 out_inserted_sd:
1982 inode->i_nlink = 0;
1983 th->t_trans_id = 0; /* so the caller can't use this handle later */
Al Viroc1eaa262008-12-30 02:03:58 -05001984 unlock_new_inode(inode); /* OK to do even if we hadn't locked it */
Jeff Mahoneyd9845612009-03-30 14:02:35 -04001985 iput(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001986 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
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 Mahoney995c7622009-03-30 14:02:47 -04002002static int grab_tail_page(struct inode *inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002003 struct page **page_result,
2004 struct buffer_head **bh_result)
2005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002007 /* 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 Mahoney995c7622009-03-30 14:02:47 -04002010 unsigned long index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002011 unsigned long pos = 0;
2012 unsigned long start = 0;
Jeff Mahoney995c7622009-03-30 14:02:47 -04002013 unsigned long blocksize = inode->i_sb->s_blocksize;
2014 unsigned long offset = (inode->i_size) & (PAGE_CACHE_SIZE - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002015 struct buffer_head *bh;
2016 struct buffer_head *head;
2017 struct page *page;
2018 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002020 /* 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 Torvalds1da177e2005-04-16 15:20:36 -07002027 }
Jeff Mahoney995c7622009-03-30 14:02:47 -04002028 page = grab_cache_page(inode->i_mapping, index);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002029 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 Torvalds1da177e2005-04-16 15:20:36 -07002035
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002036 error = block_prepare_write(page, start, offset,
2037 reiserfs_get_block_create_0);
2038 if (error)
2039 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002041 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 Torvalds1da177e2005-04-16 15:20:36 -07002050
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002051 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 Mahoney995c7622009-03-30 14:02:47 -04002057 reiserfs_error(inode->i_sb, "clm-6000",
Jeff Mahoney0030b642009-03-30 14:02:28 -04002058 "error reading block %lu", bh->b_blocknr);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002059 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 Torvalds1da177e2005-04-16 15:20:36 -07002072}
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 Mahoney995c7622009-03-30 14:02:47 -04002080int reiserfs_truncate_file(struct inode *inode, int update_timestamps)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002081{
2082 struct reiserfs_transaction_handle th;
2083 /* we want the offset for the first byte after the end of the file */
Jeff Mahoney995c7622009-03-30 14:02:47 -04002084 unsigned long offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
2085 unsigned blocksize = inode->i_sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002086 unsigned length;
2087 struct page *page = NULL;
2088 int error;
2089 struct buffer_head *bh = NULL;
Jeff Mahoney24996042005-12-14 14:38:05 -05002090 int err2;
Frederic Weisbecker22c963a2009-04-14 05:34:24 +02002091 int lock_depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Frederic Weisbecker22c963a2009-04-14 05:34:24 +02002093 lock_depth = reiserfs_write_lock_once(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Jeff Mahoney995c7622009-03-30 14:02:47 -04002095 if (inode->i_size > 0) {
2096 error = grab_tail_page(inode, &page, &bh);
2097 if (error) {
Jeff Mahoney0222e652009-03-30 14:02:44 -04002098 // -ENOENT means we truncated past the end of the file,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002099 // and get_block_create_0 could not find a block to read in,
2100 // which is ok.
2101 if (error != -ENOENT)
Jeff Mahoney995c7622009-03-30 14:02:47 -04002102 reiserfs_error(inode->i_sb, "clm-6001",
Jeff Mahoney0030b642009-03-30 14:02:28 -04002103 "grab_tail_page failed %d",
2104 error);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002105 page = NULL;
2106 bh = NULL;
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Jeff Mahoney0222e652009-03-30 14:02:44 -04002110 /* 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 Torvaldsbd4c6252005-07-12 20:21:28 -07002115 ** (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 Mahoney995c7622009-03-30 14:02:47 -04002120 error = journal_begin(&th, inode->i_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002121 JOURNAL_PER_BALANCE_CNT * 2 + 1);
2122 if (error)
2123 goto out;
Jeff Mahoney995c7622009-03-30 14:02:47 -04002124 reiserfs_update_inode_transaction(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002125 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 Mahoney995c7622009-03-30 14:02:47 -04002129 add_save_link(&th, inode, 1);
2130 err2 = reiserfs_do_truncate(&th, inode, page, update_timestamps);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002131 error =
Jeff Mahoney995c7622009-03-30 14:02:47 -04002132 journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 2 + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002133 if (error)
2134 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Jeff Mahoney24996042005-12-14 14:38:05 -05002136 /* check reiserfs_do_truncate after ending the transaction */
2137 if (err2) {
2138 error = err2;
2139 goto out;
2140 }
2141
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002142 if (update_timestamps) {
Jeff Mahoney995c7622009-03-30 14:02:47 -04002143 error = remove_save_link(inode, 1 /* truncate */);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002144 if (error)
2145 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002148 if (page) {
2149 length = offset & (blocksize - 1);
2150 /* if we are not on a block boundary */
2151 if (length) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002152 length = blocksize - length;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002153 zero_user(page, offset, length);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002154 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 Weisbecker22c963a2009-04-14 05:34:24 +02002162 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
2163
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002164 return 0;
2165 out:
2166 if (page) {
2167 unlock_page(page);
2168 page_cache_release(page);
2169 }
Frederic Weisbecker22c963a2009-04-14 05:34:24 +02002170
2171 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
2172
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002173 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002176static 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 Drokin7729ac52005-11-28 13:43:53 -08002190 loff_t byte_offset = ((loff_t)block << inode->i_sb->s_blocksize_bits)+1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002191 int retval;
2192 int use_get_block = 0;
2193 int bytes_copied = 0;
2194 int copy_size;
2195 int trans_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002197 /* catch places below that try to log something without starting a trans */
2198 th.t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002200 if (!buffer_uptodate(bh_result)) {
2201 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002204 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 Torvalds1da177e2005-04-16 15:20:36 -07002208
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002209 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 Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002216 bh = get_last_bh(&path);
2217 ih = get_ih(&path);
2218 item = get_item(&path);
2219 pos_in_item = path.pos_in_item;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002221 /* we've found an unformatted node */
2222 if (indirect_item_found(retval, ih)) {
2223 if (bytes_copied > 0) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002224 reiserfs_warning(inode->i_sb, "clm-6002",
2225 "bytes_copied %d", bytes_copied);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002226 }
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 Torvalds1da177e2005-04-16 15:20:36 -07002239
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002240 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 Mahoney45b03d52009-03-30 14:02:21 -04002282 reiserfs_warning(inode->i_sb, "clm-6003",
2283 "bad item inode %lu", inode->i_ino);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002284 retval = -EIO;
2285 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002287 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002289 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 Torvalds1da177e2005-04-16 15:20:36 -07002296 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002297 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002299 /* 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 Sorensen1b1dcc12006-01-09 15:59:24 -08002302 GET_BLOCK_CREATE | GET_BLOCK_NO_IMUX
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002303 | 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 Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
Jeff Mahoney0222e652009-03-30 14:02:44 -04002326/*
2327 * mason@suse.com: updated in 2.5.54 to follow the same general io
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 * start/recovery path as __block_write_full_page, along with special
2329 * code to handle reiserfs tails.
2330 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002331static 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 Masonb4c76fa2006-08-05 12:15:10 -07002338 sector_t last_block;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002339 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 Torvalds1da177e2005-04-16 15:20:36 -07002347
Chris Masone0e851c2006-02-01 03:06:49 -08002348 /* 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 Torvaldsbd4c6252005-07-12 20:21:28 -07002355 /* 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 Torvalds1da177e2005-04-16 15:20:36 -07002363 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002364 head = page_buffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002366 /* 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 Torvaldsbd4c6252005-07-12 20:21:28 -07002370 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 Lametereebd2aa2008-02-04 22:28:29 -08002378 zero_user_segment(page, last_offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002380 bh = head;
2381 block = page->index << (PAGE_CACHE_SHIFT - s->s_blocksize_bits);
Chris Masonb4c76fa2006-08-05 12:15:10 -07002382 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002383 /* first map all the buffers, logging any direct items we find */
2384 do {
Chris Masonb4c76fa2006-08-05 12:15:10 -07002385 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 Torvaldsbd4c6252005-07-12 20:21:28 -07002395 && 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 Pigginca5de402008-08-02 12:02:13 +02002444 if (!trylock_buffer(bh)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002445 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 Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 if (checked) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002457 error = journal_end(&th, s, bh_per_page + 1);
2458 reiserfs_write_unlock(s);
2459 if (error)
2460 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002462 BUG_ON(PageWriteback(page));
2463 set_page_writeback(page);
2464 unlock_page(page);
2465
2466 /*
Jeff Mahoney0222e652009-03-30 14:02:44 -04002467 * since any buffer might be the only dirty buffer on the page,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002468 * the first submit_bh can bring the page out of writeback.
2469 * be careful with the buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 do {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002472 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 Torvalds1da177e2005-04-16 15:20:36 -07002480
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002481 error = 0;
2482 done:
2483 if (nr == 0) {
2484 /*
2485 * if this page only had a direct item, it is very possible for
Jeff Mahoney0222e652009-03-30 14:02:44 -04002486 * no io to be required without there being an error. Or,
2487 * someone else could have locked them and sent them down the
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002488 * 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 Torvalds1da177e2005-04-16 15:20:36 -07002501 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002502 return error;
2503
2504 fail:
2505 /* catches various errors, we need to make sure any valid dirty blocks
Jeff Mahoney0222e652009-03-30 14:02:44 -04002506 * get to the media. The page is currently locked and not marked for
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002507 * 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 Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002542static int reiserfs_readpage(struct file *f, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002544 return block_read_full_page(page, reiserfs_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002547static int reiserfs_writepage(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002549 struct inode *inode = page->mapping->host;
2550 reiserfs_wait_on_write_block(inode->i_sb);
2551 return reiserfs_write_full_page(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
Jan Karaec8e2f72009-12-17 15:27:06 -08002554static 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 Savelievba9d8ce2007-10-16 01:25:14 -07002560static 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 Savelievf7557e82007-10-16 01:25:14 -07002571 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 Savelievba9d8ce2007-10-16 01:25:14 -07002579 index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin54566b22009-01-04 12:00:53 -08002580 page = grab_cache_page_write_begin(mapping, index, flags);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002581 if (!page)
2582 return -ENOMEM;
2583 *pagep = page;
2584
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002585 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 Hellwig6e1db882010-06-04 11:29:57 +02002596 ret = __block_write_begin(page, pos, len, reiserfs_get_block);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002597 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 Karaec8e2f72009-12-17 15:27:06 -08002625 /* Truncate allocated blocks */
2626 reiserfs_truncate_failed_write(inode);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002627 }
2628 return ret;
2629}
2630
2631int reiserfs_prepare_write(struct file *f, struct page *page,
2632 unsigned from, unsigned to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002634 struct inode *inode = page->mapping->host;
2635 int ret;
2636 int old_ref = 0;
2637
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002638 reiserfs_write_unlock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002639 reiserfs_wait_on_write_block(inode->i_sb);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002640 reiserfs_write_lock(inode->i_sb);
2641
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002642 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
2683static 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 Savelievba9d8ce2007-10-16 01:25:14 -07002688static 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 Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002697 int lock_depth = 0;
2698 bool locked = false;
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002699
Vladimir Savelievf7557e82007-10-16 01:25:14 -07002700 if ((unsigned long)fsdata & AOP_FLAG_CONT_EXPAND)
2701 pos ++;
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002702
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 Karaec8e2f72009-12-17 15:27:06 -08002724 if (pos + copied > inode->i_size) {
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002725 struct reiserfs_transaction_handle myth;
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002726 lock_depth = reiserfs_write_lock_once(inode->i_sb);
2727 locked = true;
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002728 /* 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 Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002738 if (ret)
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002739 goto journal_error;
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002740
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002741 reiserfs_update_inode_transaction(inode);
Jan Karaec8e2f72009-12-17 15:27:06 -08002742 inode->i_size = pos + copied;
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002743 /*
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 Savelievba9d8ce2007-10-16 01:25:14 -07002752 if (ret)
2753 goto journal_error;
2754 }
2755 if (th) {
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002756 if (!locked) {
2757 lock_depth = reiserfs_write_lock_once(inode->i_sb);
2758 locked = true;
2759 }
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002760 if (!update_sd)
2761 mark_inode_dirty(inode);
2762 ret = reiserfs_end_persistent_transaction(th);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002763 if (ret)
2764 goto out;
2765 }
2766
2767 out:
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002768 if (locked)
2769 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002770 unlock_page(page);
2771 page_cache_release(page);
Jan Karaec8e2f72009-12-17 15:27:06 -08002772
2773 if (pos + len > inode->i_size)
2774 reiserfs_truncate_failed_write(inode);
2775
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002776 return ret == 0 ? copied : ret;
2777
2778 journal_error:
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002779 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
2780 locked = false;
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002781 if (th) {
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002782 if (!update_sd)
2783 reiserfs_update_sd(th, inode);
2784 ret = reiserfs_end_persistent_transaction(th);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002785 }
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002786 goto out;
2787}
2788
2789int reiserfs_commit_write(struct file *f, struct page *page,
2790 unsigned from, unsigned to)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002791{
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 Weisbecker8ebc4232009-04-07 04:19:49 +02002798 reiserfs_write_unlock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002799 reiserfs_wait_on_write_block(inode->i_sb);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002800 reiserfs_write_lock(inode->i_sb);
2801
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002802 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 Torvaldsbd4c6252005-07-12 20:21:28 -07002813 /* 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 Weisbecker7e942772009-08-25 03:38:12 +02002823 if (ret)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002824 goto journal_error;
Frederic Weisbecker7e942772009-08-25 03:38:12 +02002825
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002826 reiserfs_update_inode_transaction(inode);
2827 inode->i_size = pos;
Chris Mason9f037832005-09-13 01:25:17 -07002828 /*
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 Torvaldsbd4c6252005-07-12 20:21:28 -07002834 reiserfs_update_sd(&myth, inode);
2835 update_sd = 1;
2836 ret = journal_end(&myth, inode->i_sb, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002837 if (ret)
2838 goto journal_error;
2839 }
2840 if (th) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002841 if (!update_sd)
Chris Mason9f037832005-09-13 01:25:17 -07002842 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002843 ret = reiserfs_end_persistent_transaction(th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002844 if (ret)
2845 goto out;
2846 }
2847
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002848 out:
2849 return ret;
2850
2851 journal_error:
2852 if (th) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002853 if (!update_sd)
2854 reiserfs_update_sd(th, inode);
2855 ret = reiserfs_end_persistent_transaction(th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002856 }
2857
2858 return ret;
2859}
2860
2861void 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 Torvalds1da177e2005-04-16 15:20:36 -07002866 else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002867 inode->i_flags &= ~S_SYNC;
2868 if (sd_attrs & REISERFS_IMMUTABLE_FL)
2869 inode->i_flags |= S_IMMUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002871 inode->i_flags &= ~S_IMMUTABLE;
2872 if (sd_attrs & REISERFS_APPEND_FL)
2873 inode->i_flags |= S_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002875 inode->i_flags &= ~S_APPEND;
2876 if (sd_attrs & REISERFS_NOATIME_FL)
2877 inode->i_flags |= S_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002879 inode->i_flags &= ~S_NOATIME;
2880 if (sd_attrs & REISERFS_NOTAIL_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 REISERFS_I(inode)->i_flags |= i_nopack_mask;
2882 else
2883 REISERFS_I(inode)->i_flags &= ~i_nopack_mask;
2884 }
2885}
2886
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002887void i_attrs_to_sd_attrs(struct inode *inode, __u16 * sd_attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002889 if (reiserfs_attrs(inode->i_sb)) {
2890 if (inode->i_flags & S_IMMUTABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 *sd_attrs |= REISERFS_IMMUTABLE_FL;
2892 else
2893 *sd_attrs &= ~REISERFS_IMMUTABLE_FL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002894 if (inode->i_flags & S_SYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 *sd_attrs |= REISERFS_SYNC_FL;
2896 else
2897 *sd_attrs &= ~REISERFS_SYNC_FL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002898 if (inode->i_flags & S_NOATIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 *sd_attrs |= REISERFS_NOATIME_FL;
2900 else
2901 *sd_attrs &= ~REISERFS_NOATIME_FL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002902 if (REISERFS_I(inode)->i_flags & i_nopack_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 *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*/
2912static int invalidatepage_can_drop(struct inode *inode, struct buffer_head *bh)
2913{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002914 int ret = 1;
2915 struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Chris Masond62b1b82006-02-01 03:06:47 -08002917 lock_buffer(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002918 spin_lock(&j->j_dirty_buffers_lock);
2919 if (!buffer_mapped(bh)) {
2920 goto free_jh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002922 /* the page is locked, and the only places that log a data buffer
2923 * also lock the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002925 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 Masond62b1b82006-02-01 03:06:47 -08002933 } else if (buffer_dirty(bh)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002934 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 Masond62b1b82006-02-01 03:06:47 -08002959 unlock_buffer(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002960 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961}
2962
2963/* clm -- taken from fs/buffer.c:block_invalidate_page */
NeilBrown2ff28e22006-03-26 01:37:18 -08002964static void reiserfs_invalidatepage(struct page *page, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002966 struct buffer_head *head, *bh, *next;
2967 struct inode *inode = page->mapping->host;
2968 unsigned int curr_off = 0;
2969 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002971 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002973 if (offset == 0)
2974 ClearPageChecked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002976 if (!page_has_buffers(page))
2977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002979 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 Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 /*
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002999 * 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 Torvalds1da177e2005-04-16 15:20:36 -07003002 */
NeilBrown2ff28e22006-03-26 01:37:18 -08003003 if (!offset && ret) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003004 ret = try_to_release_page(page, 0);
NeilBrown2ff28e22006-03-26 01:37:18 -08003005 /* maybe should BUG_ON(!ret); - neilb */
3006 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003007 out:
NeilBrown2ff28e22006-03-26 01:37:18 -08003008 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003011static 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 Torvalds1da177e2005-04-16 15:20:36 -07003019}
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 Viro27496a82005-10-21 03:20:48 -04003030static int reiserfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003032 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 Torvalds1da177e2005-04-16 15:20:36 -07003037
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003038 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 Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
3059/* We thank Mingming Cao for helping us understand in great detail what
3060 to do in this section of the code. */
3061static ssize_t reiserfs_direct_IO(int rw, struct kiocb *iocb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003062 const struct iovec *iov, loff_t offset,
3063 unsigned long nr_segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003065 struct file *file = iocb->ki_filp;
3066 struct inode *inode = file->f_mapping->host;
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003067 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003069 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003070 offset, nr_segs,
3071 reiserfs_get_blocks_direct_io, NULL);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003072
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 Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003088int reiserfs_setattr(struct dentry *dentry, struct iattr *attr)
3089{
3090 struct inode *inode = dentry->d_inode;
Jeff Laytoncdd6fe62007-10-18 03:05:19 -07003091 unsigned int ia_valid;
Frederic Weisbecker5fe1533f2010-01-04 22:04:01 +01003092 int depth;
3093 int error;
Jeff Laytoncdd6fe62007-10-18 03:05:19 -07003094
Christoph Hellwigdb78b872010-06-04 11:30:03 +02003095 error = inode_change_ok(inode, attr);
3096 if (error)
3097 return error;
3098
Jeff Laytoncdd6fe62007-10-18 03:05:19 -07003099 /* must be turned off for recursive notify_change calls */
3100 ia_valid = attr->ia_valid &= ~(ATTR_KILL_SUID|ATTR_KILL_SGID);
3101
Frederic Weisbecker5fe1533f2010-01-04 22:04:01 +01003102 depth = reiserfs_write_lock_once(inode->i_sb);
Dmitry Monakhov12755622010-04-08 22:04:20 +04003103 if (is_quota_modification(inode, attr))
Christoph Hellwig871a2932010-03-03 09:05:07 -05003104 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05003105
Dmitry Monakhov12755622010-04-08 22:04:20 +04003106 if (attr->ia_valid & ATTR_SIZE) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003107 /* 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 Torvalds1da177e2005-04-16 15:20:36 -07003113 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003115 /* fill in hole pointers in the expanding truncate case. */
3116 if (attr->ia_size > inode->i_size) {
Vladimir Savelievf7557e82007-10-16 01:25:14 -07003117 error = generic_cont_expand_simple(inode, attr->ia_size);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003118 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 Savelievdd535a52006-07-01 04:36:32 -07003132 /*
3133 * file size is changed, ctime and mtime are
3134 * to be updated
3135 */
3136 attr->ia_valid |= (ATTR_MTIME | ATTR_CTIME);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003137 }
3138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003140 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 Torvalds1da177e2005-04-16 15:20:36 -07003147
Christoph Hellwig10257742010-06-04 11:30:02 +02003148 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 Torvalds1da177e2005-04-16 15:20:36 -07003156
Christoph Hellwig10257742010-06-04 11:30:02 +02003157 error = reiserfs_chown_xattrs(inode, attr);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003158
Christoph Hellwig10257742010-06-04 11:30:02 +02003159 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 Torvaldsbd4c6252005-07-12 20:21:28 -07003170 }
Christoph Hellwig10257742010-06-04 11:30:02 +02003171
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 Torvaldsbd4c6252005-07-12 20:21:28 -07003182 }
3183
Christoph Hellwig10257742010-06-04 11:30:02 +02003184 /*
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 Torvaldsbd4c6252005-07-12 20:21:28 -07003201 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 Weisbecker5fe1533f2010-01-04 22:04:01 +01003207 reiserfs_write_unlock_once(inode->i_sb, depth);
3208
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003209 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003212const struct address_space_operations reiserfs_address_space_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003213 .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 Savelievba9d8ce2007-10-16 01:25:14 -07003219 .write_begin = reiserfs_write_begin,
3220 .write_end = reiserfs_write_end,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003221 .bmap = reiserfs_aop_bmap,
3222 .direct_IO = reiserfs_direct_IO,
3223 .set_page_dirty = reiserfs_set_page_dirty,
3224};