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