davinci: am18x/da850/omap-l138: keep async clock constant with cpufreq
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / reiserfs / inode.c
blobcaa758377d66b8ba5530d029635d8cdfa98048ba
1 /*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
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>
10 #include <linux/exportfs.h>
11 #include <linux/smp_lock.h>
12 #include <linux/pagemap.h>
13 #include <linux/highmem.h>
14 #include <linux/slab.h>
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>
21 #include <linux/swap.h>
23 int reiserfs_commit_write(struct file *f, struct page *page,
24 unsigned from, unsigned to);
25 int reiserfs_prepare_write(struct file *f, struct page *page,
26 unsigned from, unsigned to);
28 void reiserfs_evict_inode(struct inode *inode)
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;
35 int depth;
36 int err;
38 if (!inode->i_nlink && !is_bad_inode(inode))
39 dquot_initialize(inode);
41 truncate_inode_pages(&inode->i_data, 0);
42 if (inode->i_nlink)
43 goto no_delete;
45 depth = reiserfs_write_lock_once(inode->i_sb);
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 */
49 reiserfs_delete_xattrs(inode);
51 if (journal_begin(&th, inode->i_sb, jbegin_count))
52 goto out;
53 reiserfs_update_inode_transaction(inode);
55 reiserfs_discard_prealloc(&th, inode);
57 err = reiserfs_delete_object(&th, inode);
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 */
62 if (!err)
63 dquot_free_inode(inode);
65 if (journal_end(&th, inode->i_sb, jbegin_count))
66 goto out;
68 /* check return value from reiserfs_delete_object after
69 * ending the transaction
71 if (err)
72 goto out;
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 */
81 out:
82 end_writeback(inode); /* note this must go after the journal_end to prevent deadlock */
83 dquot_drop(inode);
84 inode->i_blocks = 0;
85 reiserfs_write_unlock_once(inode->i_sb, depth);
86 return;
88 no_delete:
89 end_writeback(inode);
90 dquot_drop(inode);
93 static void _make_cpu_key(struct cpu_key *key, int version, __u32 dirid,
94 __u32 objectid, loff_t offset, int type, int length)
96 key->version = version;
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;
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 */
107 void make_cpu_key(struct cpu_key *key, struct inode *inode, loff_t offset,
108 int type, int length)
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);
117 // when key is 0, do not set version and short key
119 inline void make_le_item_head(struct item_head *ih, const struct cpu_key *key,
120 int version,
121 loff_t offset, int type, int length,
122 int entry_count /*or ih_free_space */ )
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);
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);
140 // FIXME: we might cache recently accessed indirect item
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....
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.
155 ** The size of the cache could be dynamic based on the size of the file.
157 ** I'd also like to see us cache the location the stat data item, since
158 ** we are needlessly researching for that frequently.
160 ** --chris
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
171 static inline void fix_tail_page_for_writing(struct page *page)
173 struct buffer_head *head, *next, *bh;
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);
183 bh = next;
184 } while (bh != head);
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 */
190 static inline int allocation_needed(int retval, b_blocknr_t allocated,
191 struct item_head *ih,
192 __le32 * item, int pos_in_item)
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;
202 static inline int indirect_item_found(int retval, struct item_head *ih)
204 return (retval == POSITION_FOUND) && is_indirect_le_ih(ih);
207 static inline void set_block_dev_mapped(struct buffer_head *bh,
208 b_blocknr_t block, struct inode *inode)
210 map_bh(bh, inode->i_sb, block);
214 // files which were created in the earlier version can not be longer,
215 // than 2 gb
217 static int file_capable(struct inode *inode, sector_t block)
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;
223 return 0;
226 static int restart_transaction(struct reiserfs_transaction_handle *th,
227 struct inode *inode, struct treepath *path)
229 struct super_block *s = th->t_super;
230 int len = th->t_blocks_allocated;
231 int err;
233 BUG_ON(!th->t_trans_id);
234 BUG_ON(!th->t_refcount);
236 pathrelse(path);
238 /* we cannot restart while nested */
239 if (th->t_refcount > 1) {
240 return 0;
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);
249 return err;
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)
257 // Please improve the english/clarity in the comment above, as it is
258 // hard to understand.
260 static int _get_block_create_0(struct inode *inode, sector_t block,
261 struct buffer_head *bh_result, int args)
263 INITIALIZE_PATH(path);
264 struct cpu_key key;
265 struct buffer_head *bh;
266 struct item_head *ih, tmp_ih;
267 b_blocknr_t blocknr;
268 char *p = NULL;
269 int chars;
270 int ret;
271 int result;
272 int done = 0;
273 unsigned long offset;
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,
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;
293 return 0;
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);
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);
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;
320 pathrelse(&path);
321 if (p)
322 kunmap(bh_result->b_page);
323 return ret;
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;
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
339 if (buffer_uptodate(bh_result)) {
340 goto finished;
341 } else
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
348 if (!bh_result->b_page || PageUptodate(bh_result->b_page)) {
349 set_buffer_uptodate(bh_result);
350 goto finished;
352 // read file tail into part of page
353 offset = (cpu_key_k_offset(&key) - 1) & (PAGE_CACHE_SIZE - 1);
354 copy_item_head(&tmp_ih, ih);
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
361 if (!p)
362 p = (char *)kmap(bh_result->b_page);
364 p += offset;
365 memset(p, 0, inode->i_sb->s_blocksize);
366 do {
367 if (!is_direct_le_ih(ih)) {
368 BUG();
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
372 ** correct, and when direct item padding results in a few
373 ** extra bytes at the end of the direct item
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;
385 memcpy(p, B_I_PITEM(bh, ih) + path.pos_in_item, chars);
387 if (done)
388 break;
390 p += chars;
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;
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);
409 flush_dcache_page(bh_result->b_page);
410 kunmap(bh_result->b_page);
412 finished:
413 pathrelse(&path);
415 if (result == IO_ERROR)
416 return -EIO;
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
421 map_bh(bh_result, inode->i_sb, 0);
422 set_buffer_uptodate(bh_result);
423 return 0;
426 // this is called to create file map. So, _get_block_create_0 will not
427 // read direct item
428 static int reiserfs_bmap(struct inode *inode, sector_t block,
429 struct buffer_head *bh_result, int create)
431 if (!file_capable(inode, block))
432 return -EFBIG;
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;
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.
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.
453 ** The point of the wrapper is forcing a certain value for create, even
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,
456 ** don't use this function.
458 static int reiserfs_get_block_create_0(struct inode *inode, sector_t block,
459 struct buffer_head *bh_result,
460 int create)
462 return reiserfs_get_block(inode, block, bh_result, GET_BLOCK_NO_HOLE);
465 /* This is special helper for reiserfs_get_block in case we are executing
466 direct_IO request. */
467 static int reiserfs_get_blocks_direct_io(struct inode *inode,
468 sector_t iblock,
469 struct buffer_head *bh_result,
470 int create)
472 int ret;
474 bh_result->b_page = NULL;
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);
481 ret = reiserfs_get_block(inode, iblock, bh_result,
482 create | GET_BLOCK_NO_DANGLE);
483 if (ret)
484 goto out;
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
491 clear_buffer_mapped(bh_result);
492 ret = -EINVAL;
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;
499 reiserfs_write_lock(inode->i_sb);
501 err = reiserfs_commit_for_inode(inode);
502 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
504 reiserfs_write_unlock(inode->i_sb);
506 if (err < 0)
507 ret = err;
509 out:
510 return ret;
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
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.
523 static int convert_tail_for_hole(struct inode *inode,
524 struct buffer_head *bh_result,
525 loff_t tail_offset)
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;
534 if ((tail_offset & (bh_result->b_size - 1)) != 1)
535 return -EIO;
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;
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;
551 } else {
552 tail_page = hole_page;
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
557 ** must first take i_mutex.
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.
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;
569 /* tail conversion might change the data in the page */
570 flush_dcache_page(tail_page);
572 retval = reiserfs_commit_write(NULL, tail_page, tail_start, tail_end);
574 unlock:
575 if (tail_page != hole_page) {
576 unlock_page(tail_page);
577 page_cache_release(tail_page);
579 out:
580 return retval;
583 static inline int _allocate_block(struct reiserfs_transaction_handle *th,
584 sector_t block,
585 struct inode *inode,
586 b_blocknr_t * allocated_block_nr,
587 struct treepath *path, int flags)
589 BUG_ON(!th->t_trans_id);
591 #ifdef REISERFS_PREALLOCATE
592 if (!(flags & GET_BLOCK_NO_IMUX)) {
593 return reiserfs_new_unf_blocknrs2(th, inode, allocated_block_nr,
594 path, block);
596 #endif
597 return reiserfs_new_unf_blocknrs(th, inode, allocated_block_nr, path,
598 block);
601 int reiserfs_get_block(struct inode *inode, sector_t block,
602 struct buffer_head *bh_result, int create)
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;
614 int lock_depth;
615 struct reiserfs_transaction_handle *th = NULL;
616 /* space reserved in transaction batch:
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;
630 lock_depth = reiserfs_write_lock_once(inode->i_sb);
631 version = get_inode_item_key_version(inode);
633 if (!file_capable(inode, block)) {
634 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
635 return -EFBIG;
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
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);
646 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
647 return ret;
650 * if we're already in a transaction, make sure to close
651 * any new transactions we start in this func
653 if ((create & GET_BLOCK_NO_DANGLE) ||
654 reiserfs_transaction_running(inode->i_sb))
655 dangle = 0;
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
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;
666 /* set the key of the first byte in the 'block'-th block of file */
667 make_cpu_key(&key, inode, new_offset, TYPE_ANY, 3 /*key length */ );
668 if ((new_offset + inode->i_sb->s_blocksize - 1) > inode->i_size) {
669 start_trans:
670 th = reiserfs_persistent_transaction(inode->i_sb, jbegin_count);
671 if (!th) {
672 retval = -ENOMEM;
673 goto failure;
675 reiserfs_update_inode_transaction(inode);
677 research:
679 retval = search_for_position_by_key(inode->i_sb, &key, &path);
680 if (retval == IO_ERROR) {
681 retval = -EIO;
682 goto failure;
685 bh = get_last_bh(&path);
686 ih = get_ih(&path);
687 item = get_item(&path);
688 pos_in_item = path.pos_in_item;
690 fs_gen = get_generation(inode->i_sb);
691 copy_item_head(&tmp_ih, ih);
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;
701 repeat =
702 _allocate_block(th, block, inode, &allocated_block_nr,
703 &path, create);
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
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);
718 if (repeat != NO_DISK_SPACE && repeat != QUOTA_EXCEEDED) {
719 goto research;
721 if (repeat == QUOTA_EXCEEDED)
722 retval = -EDQUOT;
723 else
724 retval = -ENOSPC;
725 goto failure;
728 if (fs_changed(fs_gen, inode->i_sb)
729 && item_moved(&tmp_ih, &path)) {
730 goto research;
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;
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);
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);
764 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
766 /* the item was found, so new blocks were not added to the file
767 ** there is no need to make sure the inode is updated with this
768 ** transaction
770 return retval;
773 if (!th) {
774 pathrelse(&path);
775 goto start_trans;
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;
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 */ );
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;
801 tmp_key = key; // ;)
802 set_cpu_key_k_offset(&tmp_key, 1);
803 PATH_LAST_POSITION(&path)++;
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
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;
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 */
834 pathrelse(&path);
836 * ugly, but we can only end the transaction if
837 * we aren't nested
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;
849 retval =
850 convert_tail_for_hole(inode, bh_result,
851 tail_offset);
852 if (retval) {
853 if (retval != -ENOSPC)
854 reiserfs_error(inode->i_sb,
855 "clm-6004",
856 "convert tail failed "
857 "inode %lu, error %d",
858 inode->i_ino,
859 retval);
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,
870 goto failure;
872 goto research;
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;
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
891 set_buffer_uptodate(unbh);
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
896 if (unbh->b_page) {
897 /* we've converted the tail, so we must
898 ** flush unbh before the transaction commits
900 reiserfs_add_tail_list(inode, unbh);
902 /* mark it dirty now to prevent commit_write from adding
903 ** this buffer to the inode's dirty buffer list
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())
911 mark_buffer_dirty(unbh);
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;
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
937 RFALSE(cpu_key_k_offset(&tmp_key) > cpu_key_k_offset(&key),
938 "green-805: invalid offset");
939 blocks_needed =
941 ((cpu_key_k_offset(&key) -
942 cpu_key_k_offset(&tmp_key)) >> inode->i_sb->
943 s_blocksize_bits);
945 if (blocks_needed == 1) {
946 un = &unf_single;
947 } else {
948 un = kzalloc(min(blocks_needed, max_to_insert) * UNFM_P_SIZE, GFP_NOFS);
949 if (!un) {
950 un = &unf_single;
951 blocks_needed = 1;
952 max_to_insert = 0;
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;
971 retval =
972 reiserfs_paste_into_item(th, &path, &tmp_key, inode,
973 (char *)un,
974 UNFM_P_SIZE *
975 blocks_needed);
977 if (blocks_needed != 1)
978 kfree(un);
980 if (retval) {
981 reiserfs_free_block(th, inode,
982 allocated_block_nr, 1);
983 goto failure;
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;
994 if (done == 1)
995 break;
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
999 ** too big or too full. Update the inode so things are
1000 ** consistent if we crash before the function returns
1002 ** release the path so that anybody waiting on the path before
1003 ** ending their transaction will be able to continue.
1005 if (journal_transaction_should_end(th, th->t_blocks_allocated)) {
1006 retval = restart_transaction(th, inode, &path);
1007 if (retval)
1008 goto failure;
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.
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);
1021 retval = search_for_position_by_key(inode->i_sb, &key, &path);
1022 if (retval == IO_ERROR) {
1023 retval = -EIO;
1024 goto failure;
1026 if (retval == POSITION_FOUND) {
1027 reiserfs_warning(inode->i_sb, "vs-825",
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;
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);
1042 retval = 0;
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;
1054 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
1055 reiserfs_check_path(&path);
1056 return retval;
1059 static int
1060 reiserfs_readpages(struct file *file, struct address_space *mapping,
1061 struct list_head *pages, unsigned nr_pages)
1063 return mpage_readpages(mapping, pages, nr_pages, reiserfs_get_block);
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
1069 static int real_space_diff(struct inode *inode, int sd_size)
1071 int bytes;
1072 loff_t blocksize = inode->i_sb->s_blocksize;
1074 if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode))
1075 return sd_size;
1077 /* End of file is also in full block with indirect reference, so round
1078 ** up to the next block.
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
1085 bytes =
1086 ((inode->i_size +
1087 (blocksize - 1)) >> inode->i_sb->s_blocksize_bits) * UNFM_P_SIZE +
1088 sd_size;
1089 return bytes;
1092 static inline loff_t to_real_used_space(struct inode *inode, ulong blocks,
1093 int sd_size)
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));
1099 return ((loff_t) real_space_diff(inode, sd_size)) +
1100 (((loff_t) blocks) << 9);
1103 /* Compute number of blocks used by file in ReiserFS counting */
1104 static inline ulong to_fake_used_blocks(struct inode *inode, int sd_size)
1106 loff_t bytes = inode_get_bytes(inode);
1107 loff_t real_space = real_space_diff(inode, sd_size);
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;
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.
1118 if (bytes < real_space)
1119 return 0;
1120 return (bytes - real_space) >> 9;
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
1130 // called by read_locked_inode
1131 static void init_inode(struct inode *inode, struct treepath *path)
1133 struct buffer_head *bh;
1134 struct item_head *ih;
1135 __u32 rdev;
1136 //int version = ITEM_VERSION_1;
1138 bh = PATH_PLAST_BUFFER(path);
1139 ih = PATH_PITEM_HEAD(path);
1141 copy_key(INODE_PKEY(inode), &(ih->ih_key));
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;
1149 reiserfs_init_xattr_rwsem(inode);
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;
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;
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;
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.
1189 if (inode->i_blocks & 1) {
1190 inode->i_blocks++;
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);
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);
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));
1231 /* read persistent inode attributes from sd and initialise
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);
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));
1255 // update new stat data with inode fields
1256 static void inode2sd(void *sd, struct inode *inode, loff_t size)
1258 struct stat_data *sd_v2 = (struct stat_data *)sd;
1259 __u16 flags;
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);
1279 // used to copy inode's fields to old stat data
1280 static void inode2sd_v1(void *sd, struct inode *inode, loff_t size)
1282 struct stat_data_v1 *sd_v1 = (struct stat_data_v1 *)sd;
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);
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));
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);
1303 /* NOTE, you must prepare the buffer head before sending it here,
1304 ** and then log it after the call
1306 static void update_stat_data(struct treepath *path, struct inode *inode,
1307 loff_t size)
1309 struct buffer_head *bh;
1310 struct item_head *ih;
1312 bh = PATH_PLAST_BUFFER(path);
1313 ih = PATH_PITEM_HEAD(path);
1315 if (!is_statdata_le_ih(ih))
1316 reiserfs_panic(inode->i_sb, "vs-13065", "key %k, found item %h",
1317 INODE_PKEY(inode), ih);
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);
1326 return;
1329 void reiserfs_update_sd_size(struct reiserfs_transaction_handle *th,
1330 struct inode *inode, loff_t size)
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;
1339 BUG_ON(!th->t_trans_id);
1341 make_cpu_key(&key, inode, SD_OFFSET, TYPE_STAT_DATA, 3); //key type is unimportant
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) {
1348 reiserfs_error(inode->i_sb, "vs-13050",
1349 "i/o failure occurred trying to "
1350 "update %K stat data", &key);
1351 return;
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;
1360 reiserfs_warning(inode->i_sb, "vs-13060",
1361 "stat data of object %k (nlink == %d) "
1362 "not found (pos %d)",
1363 INODE_PKEY(inode), inode->i_nlink,
1364 pos);
1365 reiserfs_check_path(&path);
1366 return;
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
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. */
1383 break;
1385 update_stat_data(&path, inode, size);
1386 journal_mark_dirty(th, th->t_super, bh);
1387 pathrelse(&path);
1388 return;
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.
1397 static void reiserfs_make_bad_inode(struct inode *inode)
1399 memset(INODE_PKEY(inode), 0, KEY_SIZE);
1400 make_bad_inode(inode);
1404 // initially this function was derived from minix or ext2's analog and
1405 // evolved as the prototype did
1408 int reiserfs_init_locked_inode(struct inode *inode, void *p)
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;
1416 /* looks for stat data in the tree, and fills up the fields of in-core
1417 inode stat data fields */
1418 void reiserfs_read_locked_inode(struct inode *inode,
1419 struct reiserfs_iget_args *args)
1421 INITIALIZE_PATH(path_to_sd);
1422 struct cpu_key key;
1423 unsigned long dirino;
1424 int retval;
1426 dirino = args->dirid;
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;
1436 /* look for the object's stat data */
1437 retval = search_item(inode->i_sb, &key, &path_to_sd);
1438 if (retval == IO_ERROR) {
1439 reiserfs_error(inode->i_sb, "vs-13070",
1440 "i/o failure occurred trying to find "
1441 "stat data of %K", &key);
1442 reiserfs_make_bad_inode(inode);
1443 return;
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;
1453 init_inode(inode, &path_to_sd);
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
1460 Gramoulle <philippe.gramoulle@mmania.com> reproduced it.
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) {
1471 reiserfs_warning(inode->i_sb, "vs-13075",
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);
1478 reiserfs_check_path(&path_to_sd); /* init inode should be relsing */
1483 * reiserfs_find_actor() - "find actor" reiserfs supplies to iget5_locked().
1485 * @inode: inode from hash table to check
1486 * @opaque: "cookie" passed to iget5_locked(). This is &reiserfs_iget_args.
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.
1494 int reiserfs_find_actor(struct inode *inode, void *opaque)
1496 struct reiserfs_iget_args *args;
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);
1504 struct inode *reiserfs_iget(struct super_block *s, const struct cpu_key *key)
1506 struct inode *inode;
1507 struct reiserfs_iget_args args;
1509 args.objectid = key->on_disk_key.k_objectid;
1510 args.dirid = key->on_disk_key.k_dir_id;
1511 reiserfs_write_unlock(s);
1512 inode = iget5_locked(s, key->on_disk_key.k_objectid,
1513 reiserfs_find_actor, reiserfs_init_locked_inode,
1514 (void *)(&args));
1515 reiserfs_write_lock(s);
1516 if (!inode)
1517 return ERR_PTR(-ENOMEM);
1519 if (inode->i_state & I_NEW) {
1520 reiserfs_read_locked_inode(inode, &args);
1521 unlock_new_inode(inode);
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;
1529 return inode;
1532 static struct dentry *reiserfs_get_dentry(struct super_block *sb,
1533 u32 objectid, u32 dir_id, u32 generation)
1536 struct cpu_key key;
1537 struct inode *inode;
1539 key.on_disk_key.k_objectid = objectid;
1540 key.on_disk_key.k_dir_id = dir_id;
1541 reiserfs_write_lock(sb);
1542 inode = reiserfs_iget(sb, &key);
1543 if (inode && !IS_ERR(inode) && generation != 0 &&
1544 generation != inode->i_generation) {
1545 iput(inode);
1546 inode = NULL;
1548 reiserfs_write_unlock(sb);
1550 return d_obtain_alias(inode);
1553 struct dentry *reiserfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1554 int fh_len, int fh_type)
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
1568 if (fh_type > fh_len) {
1569 if (fh_type != 6 || fh_len != 5)
1570 reiserfs_warning(sb, "reiserfs-13077",
1571 "nfsd/reiserfs, fhtype=%d, len=%d - odd",
1572 fh_type, fh_len);
1573 fh_type = 5;
1576 return reiserfs_get_dentry(sb, fid->raw[0], fid->raw[1],
1577 (fh_type == 3 || fh_type >= 5) ? fid->raw[2] : 0);
1580 struct dentry *reiserfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1581 int fh_len, int fh_type)
1583 if (fh_type < 4)
1584 return NULL;
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);
1592 int reiserfs_encode_fh(struct dentry *dentry, __u32 * data, int *lenp,
1593 int need_parent)
1595 struct inode *inode = dentry->d_inode;
1596 int maxlen = *lenp;
1598 if (maxlen < 3)
1599 return 255;
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;
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;
1618 spin_unlock(&dentry->d_lock);
1619 return *lenp;
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.
1629 int reiserfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1631 struct reiserfs_transaction_handle th;
1632 int jbegin_count = 1;
1634 if (inode->i_sb->s_flags & MS_RDONLY)
1635 return -EROFS;
1636 /* memory pressure can sometimes initiate write_inode calls with sync == 1,
1637 ** these cases are just when the system needs ram, not when the
1638 ** inode needs to reach disk for safety, and they can safely be
1639 ** ignored because the altered inode has already been logged.
1641 if (wbc->sync_mode == WB_SYNC_ALL && !(current->flags & PF_MEMALLOC)) {
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);
1647 reiserfs_write_unlock(inode->i_sb);
1649 return 0;
1652 /* stat data of new object is inserted already, this inserts the item
1653 containing "." and ".." entries */
1654 static int reiserfs_new_directory(struct reiserfs_transaction_handle *th,
1655 struct inode *inode,
1656 struct item_head *ih, struct treepath *path,
1657 struct inode *dir)
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;
1665 BUG_ON(!th->t_trans_id);
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 */ );
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);
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);
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);
1692 /* look for place in the tree for new item */
1693 retval = search_item(sb, &key, path);
1694 if (retval == IO_ERROR) {
1695 reiserfs_error(sb, "vs-13080",
1696 "i/o failure occurred creating new directory");
1697 return -EIO;
1699 if (retval == ITEM_FOUND) {
1700 pathrelse(path);
1701 reiserfs_warning(sb, "vs-13070",
1702 "object with this key exists (%k)",
1703 &(ih->ih_key));
1704 return -EEXIST;
1707 /* insert item, that is empty directory item */
1708 return reiserfs_insert_item(th, path, &key, ih, inode, body);
1711 /* stat data of object has been inserted, this inserts the item
1712 containing the body of symlink */
1713 static int reiserfs_new_symlink(struct reiserfs_transaction_handle *th, struct inode *inode, /* Inode of symlink */
1714 struct item_head *ih,
1715 struct treepath *path, const char *symname,
1716 int item_len)
1718 struct super_block *sb = th->t_super;
1719 struct cpu_key key;
1720 int retval;
1722 BUG_ON(!th->t_trans_id);
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 */ );
1729 make_le_item_head(ih, NULL, KEY_FORMAT_3_5, 1, TYPE_DIRECT, item_len,
1730 0 /*free_space */ );
1732 /* look for place in the tree for new item */
1733 retval = search_item(sb, &key, path);
1734 if (retval == IO_ERROR) {
1735 reiserfs_error(sb, "vs-13080",
1736 "i/o failure occurred creating new symlink");
1737 return -EIO;
1739 if (retval == ITEM_FOUND) {
1740 pathrelse(path);
1741 reiserfs_warning(sb, "vs-13080",
1742 "object with this key exists (%k)",
1743 &(ih->ih_key));
1744 return -EEXIST;
1747 /* insert item, that is body of symlink */
1748 return reiserfs_insert_item(th, path, &key, ih, inode, symname);
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
1754 object is symlink) or nothing (if new object is regular file)
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. */
1760 int reiserfs_new_inode(struct reiserfs_transaction_handle *th,
1761 struct inode *dir, int mode, const char *symname,
1762 /* 0 for regular, EMTRY_DIR_SIZE for dirs,
1763 strlen (symname) for symlinks) */
1764 loff_t i_size, struct dentry *dentry,
1765 struct inode *inode,
1766 struct reiserfs_security_handle *security)
1768 struct super_block *sb;
1769 struct reiserfs_iget_args args;
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;
1777 BUG_ON(!th->t_trans_id);
1779 dquot_initialize(inode);
1780 err = dquot_alloc_inode(inode);
1781 if (err)
1782 goto out_end_trans;
1783 if (!dir->i_nlink) {
1784 err = -EPERM;
1785 goto out_bad_inode;
1788 sb = dir->i_sb;
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;
1797 args.objectid = inode->i_ino = le32_to_cpu(ih.ih_key.k_objectid);
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);
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;
1811 if (old_format_only(sb))
1812 /* not a perfect generation count, as object ids can be reused, but
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.
1817 inode->i_generation = le32_to_cpu(INODE_PKEY(dir)->k_objectid);
1818 else
1819 #if defined( USE_INODE_GENERATION_COUNTER )
1820 inode->i_generation =
1821 le32_to_cpu(REISERFS_SB(sb)->s_rs->s_inode_generation);
1822 #else
1823 inode->i_generation = ++event;
1824 #endif
1826 /* fill stat data */
1827 inode->i_nlink = (S_ISDIR(mode) ? 2 : 1);
1829 /* uid and gid must already be set by the caller for quota init */
1831 /* symlink cannot be immutable or append only, right? */
1832 if (S_ISLNK(inode->i_mode))
1833 inode->i_flags &= ~(S_IMMUTABLE | S_APPEND);
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 */ ;
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);
1851 reiserfs_init_xattr_rwsem(inode);
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 */ );
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;
1864 if (retval == ITEM_FOUND) {
1865 pathrelse(&path_to_key);
1866 err = -EEXIST;
1867 goto out_bad_inode;
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;
1876 inode2sd_v1(&sd, inode, inode->i_size);
1877 } else {
1878 inode2sd(&sd, inode, inode->i_size);
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)
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);
1892 /* insert the stat data into the tree */
1893 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
1894 if (REISERFS_I(dir)->new_packing_locality)
1895 th->displace_new_blocks = 1;
1896 #endif
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;
1905 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
1906 if (!th->displace_new_blocks)
1907 REISERFS_I(dir)->new_packing_locality = 0;
1908 #endif
1909 if (S_ISDIR(mode)) {
1910 /* insert item with "." and ".." */
1911 retval =
1912 reiserfs_new_directory(th, inode, &ih, &path_to_key, dir);
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);
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;
1930 if (reiserfs_posixacl(inode->i_sb)) {
1931 retval = reiserfs_inherit_default_acl(th, dir, dentry, inode);
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;
1938 } else if (inode->i_sb->s_flags & MS_POSIXACL) {
1939 reiserfs_warning(inode->i_sb, "jdm-13090",
1940 "ACLs aren't enabled in the fs, "
1941 "but vfs thinks they are!");
1942 } else if (IS_PRIVATE(dir))
1943 inode->i_flags |= S_PRIVATE;
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;
1958 reiserfs_update_sd(th, inode);
1959 reiserfs_check_path(&path_to_key);
1961 return 0;
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.
1967 out_bad_inode:
1968 /* Invalidate the object, nothing was inserted yet */
1969 INODE_PKEY(inode)->k_objectid = 0;
1971 /* Quota change must be inside a transaction for journaling */
1972 dquot_free_inode(inode);
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 */
1977 dquot_drop(inode);
1978 inode->i_flags |= S_NOQUOTA;
1979 make_bad_inode(inode);
1981 out_inserted_sd:
1982 inode->i_nlink = 0;
1983 th->t_trans_id = 0; /* so the caller can't use this handle later */
1984 unlock_new_inode(inode); /* OK to do even if we hadn't locked it */
1985 iput(inode);
1986 return err;
1990 ** finds the tail page in the page cache,
1991 ** reads the last block in.
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.
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.
2000 ** on failure, nonzero is returned, page_result and bh_result are untouched.
2002 static int grab_tail_page(struct inode *inode,
2003 struct page **page_result,
2004 struct buffer_head **bh_result)
2007 /* we want the page with the last byte in the file,
2008 ** not the page that will hold the next byte for appending
2010 unsigned long index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
2011 unsigned long pos = 0;
2012 unsigned long start = 0;
2013 unsigned long blocksize = inode->i_sb->s_blocksize;
2014 unsigned long offset = (inode->i_size) & (PAGE_CACHE_SIZE - 1);
2015 struct buffer_head *bh;
2016 struct buffer_head *head;
2017 struct page *page;
2018 int error;
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.
2025 if ((offset & (blocksize - 1)) == 0) {
2026 return -ENOENT;
2028 page = grab_cache_page(inode->i_mapping, index);
2029 error = -ENOMEM;
2030 if (!page) {
2031 goto out;
2033 /* start within the page of the last block in the file */
2034 start = (offset / blocksize) * blocksize;
2036 error = block_prepare_write(page, start, offset,
2037 reiserfs_get_block_create_0);
2038 if (error)
2039 goto unlock;
2041 head = page_buffers(page);
2042 bh = head;
2043 do {
2044 if (pos >= start) {
2045 break;
2047 bh = bh->b_this_page;
2048 pos += blocksize;
2049 } while (bh != head);
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
2057 reiserfs_error(inode->i_sb, "clm-6000",
2058 "error reading block %lu", bh->b_blocknr);
2059 error = -EIO;
2060 goto unlock;
2062 *bh_result = bh;
2063 *page_result = page;
2065 out:
2066 return error;
2068 unlock:
2069 unlock_page(page);
2070 page_cache_release(page);
2071 return error;
2075 ** vfs version of truncate file. Must NOT be called with
2076 ** a transaction already started.
2078 ** some code taken from block_truncate_page
2080 int reiserfs_truncate_file(struct inode *inode, int update_timestamps)
2082 struct reiserfs_transaction_handle th;
2083 /* we want the offset for the first byte after the end of the file */
2084 unsigned long offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
2085 unsigned blocksize = inode->i_sb->s_blocksize;
2086 unsigned length;
2087 struct page *page = NULL;
2088 int error;
2089 struct buffer_head *bh = NULL;
2090 int err2;
2091 int lock_depth;
2093 lock_depth = reiserfs_write_lock_once(inode->i_sb);
2095 if (inode->i_size > 0) {
2096 error = grab_tail_page(inode, &page, &bh);
2097 if (error) {
2098 // -ENOENT means we truncated past the end of the file,
2099 // and get_block_create_0 could not find a block to read in,
2100 // which is ok.
2101 if (error != -ENOENT)
2102 reiserfs_error(inode->i_sb, "clm-6001",
2103 "grab_tail_page failed %d",
2104 error);
2105 page = NULL;
2106 bh = NULL;
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
2115 ** (it will unmap bh if it packs).
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 */
2120 error = journal_begin(&th, inode->i_sb,
2121 JOURNAL_PER_BALANCE_CNT * 2 + 1);
2122 if (error)
2123 goto out;
2124 reiserfs_update_inode_transaction(inode);
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 */
2129 add_save_link(&th, inode, 1);
2130 err2 = reiserfs_do_truncate(&th, inode, page, update_timestamps);
2131 error =
2132 journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 2 + 1);
2133 if (error)
2134 goto out;
2136 /* check reiserfs_do_truncate after ending the transaction */
2137 if (err2) {
2138 error = err2;
2139 goto out;
2142 if (update_timestamps) {
2143 error = remove_save_link(inode, 1 /* truncate */);
2144 if (error)
2145 goto out;
2148 if (page) {
2149 length = offset & (blocksize - 1);
2150 /* if we are not on a block boundary */
2151 if (length) {
2152 length = blocksize - length;
2153 zero_user(page, offset, length);
2154 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
2155 mark_buffer_dirty(bh);
2158 unlock_page(page);
2159 page_cache_release(page);
2162 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
2164 return 0;
2165 out:
2166 if (page) {
2167 unlock_page(page);
2168 page_cache_release(page);
2171 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
2173 return error;
2176 static int map_block_for_writepage(struct inode *inode,
2177 struct buffer_head *bh_result,
2178 unsigned long block)
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;
2190 loff_t byte_offset = ((loff_t)block << inode->i_sb->s_blocksize_bits)+1;
2191 int retval;
2192 int use_get_block = 0;
2193 int bytes_copied = 0;
2194 int copy_size;
2195 int trans_running = 0;
2197 /* catch places below that try to log something without starting a trans */
2198 th.t_trans_id = 0;
2200 if (!buffer_uptodate(bh_result)) {
2201 return -EIO;
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);
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;
2216 bh = get_last_bh(&path);
2217 ih = get_ih(&path);
2218 item = get_item(&path);
2219 pos_in_item = path.pos_in_item;
2221 /* we've found an unformatted node */
2222 if (indirect_item_found(retval, ih)) {
2223 if (bytes_copied > 0) {
2224 reiserfs_warning(inode->i_sb, "clm-6002",
2225 "bytes_copied %d", bytes_copied);
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;
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;
2240 fs_gen = get_generation(inode->i_sb);
2241 copy_item_head(&tmp_ih, ih);
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;
2258 reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
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;
2266 memcpy(B_I_PITEM(bh, ih) + pos_in_item, p + bytes_copied,
2267 copy_size);
2269 journal_mark_dirty(&th, inode->i_sb, bh);
2270 bytes_copied += copy_size;
2271 set_block_dev_mapped(bh_result, 0, inode);
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;
2281 } else {
2282 reiserfs_warning(inode->i_sb, "clm-6003",
2283 "bad item inode %lu", inode->i_ino);
2284 retval = -EIO;
2285 goto out;
2287 retval = 0;
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;
2297 reiserfs_write_unlock(inode->i_sb);
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,
2302 GET_BLOCK_CREATE | GET_BLOCK_NO_IMUX
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;
2313 kunmap(bh_result->b_page);
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.
2319 lock_buffer(bh_result);
2320 clear_buffer_dirty(bh_result);
2321 unlock_buffer(bh_result);
2323 return retval;
2327 * mason@suse.com: updated in 2.5.54 to follow the same general io
2328 * start/recovery path as __block_write_full_page, along with special
2329 * code to handle reiserfs tails.
2331 static int reiserfs_write_full_page(struct page *page,
2332 struct writeback_control *wbc)
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;
2338 sector_t last_block;
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;
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;
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.
2360 if (!page_has_buffers(page)) {
2361 create_empty_buffers(page, s->s_blocksize,
2362 (1 << BH_Dirty) | (1 << BH_Uptodate));
2364 head = page_buffers(page);
2366 /* last page in the file, zero out any contents past the
2367 ** last byte in the file
2369 if (page->index >= end_index) {
2370 unsigned last_offset;
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;
2378 zero_user_segment(page, last_offset, PAGE_CACHE_SIZE);
2380 bh = head;
2381 block = page->index << (PAGE_CACHE_SHIFT - s->s_blocksize_bits);
2382 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
2383 /* first map all the buffers, logging any direct items we find */
2384 do {
2385 if (block > last_block) {
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
2391 clear_buffer_dirty(bh);
2392 set_buffer_uptodate(bh);
2393 } else if ((checked || buffer_dirty(bh)) &&
2394 (!buffer_mapped(bh) || (buffer_mapped(bh)
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
2401 if ((error = map_block_for_writepage(inode, bh, block))) {
2402 goto fail;
2405 bh = bh->b_this_page;
2406 block++;
2407 } while (bh != head);
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
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;
2423 reiserfs_update_inode_transaction(inode);
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;
2433 if (checked) {
2434 reiserfs_prepare_for_journal(s, bh, 1);
2435 journal_mark_dirty(&th, s, bh);
2436 continue;
2438 /* from this point on, we know the buffer is mapped to a
2439 * real block and not a direct item
2441 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
2442 lock_buffer(bh);
2443 } else {
2444 if (!trylock_buffer(bh)) {
2445 redirty_page_for_writepage(wbc, page);
2446 continue;
2449 if (test_clear_buffer_dirty(bh)) {
2450 mark_buffer_async_write(bh);
2451 } else {
2452 unlock_buffer(bh);
2454 } while ((bh = bh->b_this_page) != head);
2456 if (checked) {
2457 error = journal_end(&th, s, bh_per_page + 1);
2458 reiserfs_write_unlock(s);
2459 if (error)
2460 goto fail;
2462 BUG_ON(PageWriteback(page));
2463 set_page_writeback(page);
2464 unlock_page(page);
2467 * since any buffer might be the only dirty buffer on the page,
2468 * the first submit_bh can bring the page out of writeback.
2469 * be careful with the buffers.
2471 do {
2472 struct buffer_head *next = bh->b_this_page;
2473 if (buffer_async_write(bh)) {
2474 submit_bh(WRITE, bh);
2475 nr++;
2477 put_bh(bh);
2478 bh = next;
2479 } while (bh != head);
2481 error = 0;
2482 done:
2483 if (nr == 0) {
2485 * if this page only had a direct item, it is very possible for
2486 * no io to be required without there being an error. Or,
2487 * someone else could have locked them and sent them down the
2488 * pipe without locking the page
2490 bh = head;
2491 do {
2492 if (!buffer_uptodate(bh)) {
2493 partial = 1;
2494 break;
2496 bh = bh->b_this_page;
2497 } while (bh != head);
2498 if (!partial)
2499 SetPageUptodate(page);
2500 end_page_writeback(page);
2502 return error;
2504 fail:
2505 /* catches various errors, we need to make sure any valid dirty blocks
2506 * get to the media. The page is currently locked and not marked for
2507 * writeback
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 {
2518 * clear any dirty bits that might have come from getting
2519 * attached to a dirty page
2521 clear_buffer_dirty(bh);
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++;
2536 put_bh(bh);
2537 bh = next;
2538 } while (bh != head);
2539 goto done;
2542 static int reiserfs_readpage(struct file *f, struct page *page)
2544 return block_read_full_page(page, reiserfs_get_block);
2547 static int reiserfs_writepage(struct page *page, struct writeback_control *wbc)
2549 struct inode *inode = page->mapping->host;
2550 reiserfs_wait_on_write_block(inode->i_sb);
2551 return reiserfs_write_full_page(page, wbc);
2554 static void reiserfs_truncate_failed_write(struct inode *inode)
2556 truncate_inode_pages(inode->i_mapping, inode->i_size);
2557 reiserfs_truncate_file(inode, 0);
2560 static int reiserfs_write_begin(struct file *file,
2561 struct address_space *mapping,
2562 loff_t pos, unsigned len, unsigned flags,
2563 struct page **pagep, void **fsdata)
2565 struct inode *inode;
2566 struct page *page;
2567 pgoff_t index;
2568 int ret;
2569 int old_ref = 0;
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;
2579 index = pos >> PAGE_CACHE_SHIFT;
2580 page = grab_cache_page_write_begin(mapping, index, flags);
2581 if (!page)
2582 return -ENOMEM;
2583 *pagep = page;
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++;
2596 ret = __block_write_begin(page, pos, len, reiserfs_get_block);
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.
2603 * But, if we had nested into an existing transaction, we need
2604 * to just drop the ref count on the handle.
2606 * If old_ref == 0, the transaction is from reiserfs_get_block,
2607 * and it was a persistent trans. Otherwise, it was nested above.
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;
2622 if (ret) {
2623 unlock_page(page);
2624 page_cache_release(page);
2625 /* Truncate allocated blocks */
2626 reiserfs_truncate_failed_write(inode);
2628 return ret;
2631 int reiserfs_prepare_write(struct file *f, struct page *page,
2632 unsigned from, unsigned to)
2634 struct inode *inode = page->mapping->host;
2635 int ret;
2636 int old_ref = 0;
2638 reiserfs_write_unlock(inode->i_sb);
2639 reiserfs_wait_on_write_block(inode->i_sb);
2640 reiserfs_write_lock(inode->i_sb);
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++;
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.
2660 * But, if we had nested into an existing transaction, we need
2661 * to just drop the ref count on the handle.
2663 * If old_ref == 0, the transaction is from reiserfs_get_block,
2664 * and it was a persistent trans. Otherwise, it was nested above.
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;
2679 return ret;
2683 static sector_t reiserfs_aop_bmap(struct address_space *as, sector_t block)
2685 return generic_block_bmap(as, block, reiserfs_bmap);
2688 static int reiserfs_write_end(struct file *file, struct address_space *mapping,
2689 loff_t pos, unsigned len, unsigned copied,
2690 struct page *page, void *fsdata)
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;
2697 int lock_depth = 0;
2698 bool locked = false;
2700 if ((unsigned long)fsdata & AOP_FLAG_CONT_EXPAND)
2701 pos ++;
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;
2709 start = pos & (PAGE_CACHE_SIZE - 1);
2710 if (unlikely(copied < len)) {
2711 if (!PageUptodate(page))
2712 copied = 0;
2714 page_zero_new_buffers(page, start + copied, start + len);
2716 flush_dcache_page(page);
2718 reiserfs_commit_page(inode, page, start, start + copied);
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.
2724 if (pos + copied > inode->i_size) {
2725 struct reiserfs_transaction_handle myth;
2726 lock_depth = reiserfs_write_lock_once(inode->i_sb);
2727 locked = true;
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;
2737 ret = journal_begin(&myth, inode->i_sb, 1);
2738 if (ret)
2739 goto journal_error;
2741 reiserfs_update_inode_transaction(inode);
2742 inode->i_size = pos + copied;
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
2748 mark_inode_dirty(inode);
2749 reiserfs_update_sd(&myth, inode);
2750 update_sd = 1;
2751 ret = journal_end(&myth, inode->i_sb, 1);
2752 if (ret)
2753 goto journal_error;
2755 if (th) {
2756 if (!locked) {
2757 lock_depth = reiserfs_write_lock_once(inode->i_sb);
2758 locked = true;
2760 if (!update_sd)
2761 mark_inode_dirty(inode);
2762 ret = reiserfs_end_persistent_transaction(th);
2763 if (ret)
2764 goto out;
2767 out:
2768 if (locked)
2769 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
2770 unlock_page(page);
2771 page_cache_release(page);
2773 if (pos + len > inode->i_size)
2774 reiserfs_truncate_failed_write(inode);
2776 return ret == 0 ? copied : ret;
2778 journal_error:
2779 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
2780 locked = false;
2781 if (th) {
2782 if (!update_sd)
2783 reiserfs_update_sd(th, inode);
2784 ret = reiserfs_end_persistent_transaction(th);
2786 goto out;
2789 int reiserfs_commit_write(struct file *f, struct page *page,
2790 unsigned from, unsigned to)
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;
2798 reiserfs_write_unlock(inode->i_sb);
2799 reiserfs_wait_on_write_block(inode->i_sb);
2800 reiserfs_write_lock(inode->i_sb);
2802 if (reiserfs_transaction_running(inode->i_sb)) {
2803 th = current->journal_info;
2805 reiserfs_commit_page(inode, page, from, to);
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.
2811 if (pos > inode->i_size) {
2812 struct reiserfs_transaction_handle myth;
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;
2822 ret = journal_begin(&myth, inode->i_sb, 1);
2823 if (ret)
2824 goto journal_error;
2826 reiserfs_update_inode_transaction(inode);
2827 inode->i_size = pos;
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
2833 mark_inode_dirty(inode);
2834 reiserfs_update_sd(&myth, inode);
2835 update_sd = 1;
2836 ret = journal_end(&myth, inode->i_sb, 1);
2837 if (ret)
2838 goto journal_error;
2840 if (th) {
2841 if (!update_sd)
2842 mark_inode_dirty(inode);
2843 ret = reiserfs_end_persistent_transaction(th);
2844 if (ret)
2845 goto out;
2848 out:
2849 return ret;
2851 journal_error:
2852 if (th) {
2853 if (!update_sd)
2854 reiserfs_update_sd(th, inode);
2855 ret = reiserfs_end_persistent_transaction(th);
2858 return ret;
2861 void sd_attrs_to_i_attrs(__u16 sd_attrs, struct inode *inode)
2863 if (reiserfs_attrs(inode->i_sb)) {
2864 if (sd_attrs & REISERFS_SYNC_FL)
2865 inode->i_flags |= S_SYNC;
2866 else
2867 inode->i_flags &= ~S_SYNC;
2868 if (sd_attrs & REISERFS_IMMUTABLE_FL)
2869 inode->i_flags |= S_IMMUTABLE;
2870 else
2871 inode->i_flags &= ~S_IMMUTABLE;
2872 if (sd_attrs & REISERFS_APPEND_FL)
2873 inode->i_flags |= S_APPEND;
2874 else
2875 inode->i_flags &= ~S_APPEND;
2876 if (sd_attrs & REISERFS_NOATIME_FL)
2877 inode->i_flags |= S_NOATIME;
2878 else
2879 inode->i_flags &= ~S_NOATIME;
2880 if (sd_attrs & REISERFS_NOTAIL_FL)
2881 REISERFS_I(inode)->i_flags |= i_nopack_mask;
2882 else
2883 REISERFS_I(inode)->i_flags &= ~i_nopack_mask;
2887 void i_attrs_to_sd_attrs(struct inode *inode, __u16 * sd_attrs)
2889 if (reiserfs_attrs(inode->i_sb)) {
2890 if (inode->i_flags & S_IMMUTABLE)
2891 *sd_attrs |= REISERFS_IMMUTABLE_FL;
2892 else
2893 *sd_attrs &= ~REISERFS_IMMUTABLE_FL;
2894 if (inode->i_flags & S_SYNC)
2895 *sd_attrs |= REISERFS_SYNC_FL;
2896 else
2897 *sd_attrs &= ~REISERFS_SYNC_FL;
2898 if (inode->i_flags & S_NOATIME)
2899 *sd_attrs |= REISERFS_NOATIME_FL;
2900 else
2901 *sd_attrs &= ~REISERFS_NOATIME_FL;
2902 if (REISERFS_I(inode)->i_flags & i_nopack_mask)
2903 *sd_attrs |= REISERFS_NOTAIL_FL;
2904 else
2905 *sd_attrs &= ~REISERFS_NOTAIL_FL;
2909 /* decide if this buffer needs to stay around for data logging or ordered
2910 ** write purposes
2912 static int invalidatepage_can_drop(struct inode *inode, struct buffer_head *bh)
2914 int ret = 1;
2915 struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
2917 lock_buffer(bh);
2918 spin_lock(&j->j_dirty_buffers_lock);
2919 if (!buffer_mapped(bh)) {
2920 goto free_jh;
2922 /* the page is locked, and the only places that log a data buffer
2923 * also lock the page.
2925 if (reiserfs_file_data_log(inode)) {
2927 * very conservative, leave the buffer pinned if
2928 * anyone might need it.
2930 if (buffer_journaled(bh) || buffer_journal_dirty(bh)) {
2931 ret = 0;
2933 } else if (buffer_dirty(bh)) {
2934 struct reiserfs_journal_list *jl;
2935 struct reiserfs_jh *jh = bh->b_private;
2937 /* why is this safe?
2938 * reiserfs_setattr updates i_size in the on disk
2939 * stat data before allowing vmtruncate to be called.
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.
2947 * if the buffer was put onto the ordered list for an older
2948 * transaction, we need to leave it around
2950 if (jh && (jl = jh->jl)
2951 && jl != SB_JOURNAL(inode->i_sb)->j_current_jl)
2952 ret = 0;
2954 free_jh:
2955 if (ret && bh->b_private) {
2956 reiserfs_free_jh(bh);
2958 spin_unlock(&j->j_dirty_buffers_lock);
2959 unlock_buffer(bh);
2960 return ret;
2963 /* clm -- taken from fs/buffer.c:block_invalidate_page */
2964 static void reiserfs_invalidatepage(struct page *page, unsigned long offset)
2966 struct buffer_head *head, *bh, *next;
2967 struct inode *inode = page->mapping->host;
2968 unsigned int curr_off = 0;
2969 int ret = 1;
2971 BUG_ON(!PageLocked(page));
2973 if (offset == 0)
2974 ClearPageChecked(page);
2976 if (!page_has_buffers(page))
2977 goto out;
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;
2986 * is this block fully invalidated?
2988 if (offset <= curr_off) {
2989 if (invalidatepage_can_drop(inode, bh))
2990 reiserfs_unmap_buffer(bh);
2991 else
2992 ret = 0;
2994 curr_off = next_off;
2995 bh = next;
2996 } while (bh != head);
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.
3003 if (!offset && ret) {
3004 ret = try_to_release_page(page, 0);
3005 /* maybe should BUG_ON(!ret); - neilb */
3007 out:
3008 return;
3011 static int reiserfs_set_page_dirty(struct page *page)
3013 struct inode *inode = page->mapping->host;
3014 if (reiserfs_file_data_log(inode)) {
3015 SetPageChecked(page);
3016 return __set_page_dirty_nobuffers(page);
3018 return __set_page_dirty_buffers(page);
3022 * Returns 1 if the page's buffers were dropped. The page is locked.
3024 * Takes j_dirty_buffers_lock to protect the b_assoc_buffers list_heads
3025 * in the buffers at page_buffers(page).
3027 * even in -o notail mode, we can't be sure an old mount without -o notail
3028 * didn't create files with tails.
3030 static int reiserfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
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;
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;
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;
3059 /* We thank Mingming Cao for helping us understand in great detail what
3060 to do in this section of the code. */
3061 static ssize_t reiserfs_direct_IO(int rw, struct kiocb *iocb,
3062 const struct iovec *iov, loff_t offset,
3063 unsigned long nr_segs)
3065 struct file *file = iocb->ki_filp;
3066 struct inode *inode = file->f_mapping->host;
3067 ssize_t ret;
3069 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3070 offset, nr_segs,
3071 reiserfs_get_blocks_direct_io, NULL);
3074 * In case of error extending write may have instantiated a few
3075 * blocks outside i_size. Trim these off again.
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);
3081 if (end > isize)
3082 vmtruncate(inode, isize);
3085 return ret;
3088 int reiserfs_setattr(struct dentry *dentry, struct iattr *attr)
3090 struct inode *inode = dentry->d_inode;
3091 unsigned int ia_valid;
3092 int depth;
3093 int error;
3095 error = inode_change_ok(inode, attr);
3096 if (error)
3097 return error;
3099 /* must be turned off for recursive notify_change calls */
3100 ia_valid = attr->ia_valid &= ~(ATTR_KILL_SUID|ATTR_KILL_SGID);
3102 depth = reiserfs_write_lock_once(inode->i_sb);
3103 if (is_quota_modification(inode, attr))
3104 dquot_initialize(inode);
3106 if (attr->ia_valid & ATTR_SIZE) {
3107 /* version 2 items will be caught by the s_maxbytes check
3108 ** done for us in vmtruncate
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;
3115 /* fill in hole pointers in the expanding truncate case. */
3116 if (attr->ia_size > inode->i_size) {
3117 error = generic_cont_expand_simple(inode, attr->ia_size);
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);
3127 if (err)
3128 error = err;
3130 if (error)
3131 goto out;
3133 * file size is changed, ctime and mtime are
3134 * to be updated
3136 attr->ia_valid |= (ATTR_MTIME | ATTR_CTIME);
3140 if ((((attr->ia_valid & ATTR_UID) && (attr->ia_uid & ~0xffff)) ||
3141 ((attr->ia_valid & ATTR_GID) && (attr->ia_gid & ~0xffff))) &&
3142 (get_inode_sd_version(inode) == STAT_DATA_V1)) {
3143 /* stat data of format v3.5 has 16 bit uid and gid */
3144 error = -EINVAL;
3145 goto out;
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 =
3153 (REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb) +
3154 REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb)) +
3157 error = reiserfs_chown_xattrs(inode, attr);
3159 if (error)
3160 return error;
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;
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;
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
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);
3195 if (!error) {
3196 setattr_copy(inode, attr);
3197 mark_inode_dirty(inode);
3199 depth = reiserfs_write_lock_once(inode->i_sb);
3201 if (!error && reiserfs_posixacl(inode->i_sb)) {
3202 if (attr->ia_valid & ATTR_MODE)
3203 error = reiserfs_acl_chmod(inode);
3206 out:
3207 reiserfs_write_unlock_once(inode->i_sb, depth);
3209 return error;
3212 const struct address_space_operations reiserfs_address_space_operations = {
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,
3219 .write_begin = reiserfs_write_begin,
3220 .write_end = reiserfs_write_end,
3221 .bmap = reiserfs_aop_bmap,
3222 .direct_IO = reiserfs_direct_IO,
3223 .set_page_dirty = reiserfs_set_page_dirty,