blob: bf1331a0f04b82df4376f11283ac3fa51343f836 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/time.h>
6#include <linux/fs.h>
Al Virof466c6f2012-03-17 01:16:43 -04007#include "reiserfs.h"
Al Viroa3063ab2012-03-17 01:03:10 -04008#include "acl.h"
Al Viroc45ac882012-03-17 00:59:06 -04009#include "xattr.h"
Christoph Hellwiga5694252007-07-17 04:04:28 -070010#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/pagemap.h>
12#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/uaccess.h>
15#include <asm/unaligned.h>
16#include <linux/buffer_head.h>
17#include <linux/mpage.h>
18#include <linux/writeback.h>
19#include <linux/quotaops.h>
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -070020#include <linux/swap.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070021#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -070023int reiserfs_commit_write(struct file *f, struct page *page,
24 unsigned from, unsigned to);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Al Viro845a2cc2010-06-07 11:37:37 -040026void reiserfs_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070028 /* We need blocks for transaction + (user+group) quota update (possibly delete) */
29 int jbegin_count =
30 JOURNAL_PER_BALANCE_CNT * 2 +
31 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb);
32 struct reiserfs_transaction_handle th;
Jeff Mahoney24996042005-12-14 14:38:05 -050033 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Al Viro845a2cc2010-06-07 11:37:37 -040035 if (!inode->i_nlink && !is_bad_inode(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -050036 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -050037
Mark Fashehfef26652005-09-09 13:01:31 -070038 truncate_inode_pages(&inode->i_data, 0);
Al Viro845a2cc2010-06-07 11:37:37 -040039 if (inode->i_nlink)
40 goto no_delete;
Mark Fashehfef26652005-09-09 13:01:31 -070041
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070042 /* The = 0 happens when we abort creating a new inode for some reason like lack of space.. */
43 if (!(inode->i_state & I_NEW) && INODE_PKEY(inode)->k_objectid != 0) { /* also handles bad_inode case */
Jeff Mahoney4c051412013-08-08 17:27:19 -040044 int depth;
45
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070046 reiserfs_delete_xattrs(inode);
47
Jeff Mahoney4c051412013-08-08 17:27:19 -040048 depth = reiserfs_write_lock_once(inode->i_sb);
49
Alexander Zarochentsevb0b33de2006-08-05 12:14:01 -070050 if (journal_begin(&th, inode->i_sb, jbegin_count))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070051 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070052 reiserfs_update_inode_transaction(inode);
53
Jeff Mahoneyeb35c212008-07-08 14:37:06 -040054 reiserfs_discard_prealloc(&th, inode);
55
Jeff Mahoney24996042005-12-14 14:38:05 -050056 err = reiserfs_delete_object(&th, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070057
58 /* Do quota update inside a transaction for journaled quotas. We must do that
59 * after delete_object so that quota updates go into the same transaction as
60 * stat data deletion */
Jeff Mahoney24996042005-12-14 14:38:05 -050061 if (!err)
Christoph Hellwig63936dd2010-03-03 09:05:01 -050062 dquot_free_inode(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063
Alexander Zarochentsevb0b33de2006-08-05 12:14:01 -070064 if (journal_end(&th, inode->i_sb, jbegin_count))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070065 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070066
Jeff Mahoney24996042005-12-14 14:38:05 -050067 /* check return value from reiserfs_delete_object after
68 * ending the transaction
69 */
70 if (err)
71 goto out;
72
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070073 /* all items of file are deleted, so we can remove "save" link */
74 remove_save_link(inode, 0 /* not truncate */ ); /* we can't do anything
75 * about an error here */
Jeff Mahoney4c051412013-08-08 17:27:19 -040076out:
77 reiserfs_write_unlock_once(inode->i_sb, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070078 } else {
79 /* no object items are in the tree */
80 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
Jan Karadbd57682012-05-03 14:48:02 +020082 clear_inode(inode); /* note this must go after the journal_end to prevent deadlock */
Al Viro845a2cc2010-06-07 11:37:37 -040083 dquot_drop(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084 inode->i_blocks = 0;
Sergey Senozhatskyf4ae2fa2010-08-11 14:07:01 +030085 return;
Al Viro845a2cc2010-06-07 11:37:37 -040086
87no_delete:
Jan Karadbd57682012-05-03 14:48:02 +020088 clear_inode(inode);
Al Viro845a2cc2010-06-07 11:37:37 -040089 dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070092static void _make_cpu_key(struct cpu_key *key, int version, __u32 dirid,
93 __u32 objectid, loff_t offset, int type, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070095 key->version = version;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070097 key->on_disk_key.k_dir_id = dirid;
98 key->on_disk_key.k_objectid = objectid;
99 set_cpu_key_k_offset(key, offset);
100 set_cpu_key_k_type(key, type);
101 key->key_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* take base of inode_key (it comes from inode always) (dirid, objectid) and version from an inode, set
105 offset and type of key */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700106void make_cpu_key(struct cpu_key *key, struct inode *inode, loff_t offset,
107 int type, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700109 _make_cpu_key(key, get_inode_item_key_version(inode),
110 le32_to_cpu(INODE_PKEY(inode)->k_dir_id),
111 le32_to_cpu(INODE_PKEY(inode)->k_objectid), offset, type,
112 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115//
116// when key is 0, do not set version and short key
117//
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700118inline void make_le_item_head(struct item_head *ih, const struct cpu_key *key,
119 int version,
120 loff_t offset, int type, int length,
121 int entry_count /*or ih_free_space */ )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700123 if (key) {
124 ih->ih_key.k_dir_id = cpu_to_le32(key->on_disk_key.k_dir_id);
125 ih->ih_key.k_objectid =
126 cpu_to_le32(key->on_disk_key.k_objectid);
127 }
128 put_ih_version(ih, version);
129 set_le_ih_k_offset(ih, offset);
130 set_le_ih_k_type(ih, type);
131 put_ih_item_len(ih, length);
132 /* set_ih_free_space (ih, 0); */
133 // for directory items it is entry count, for directs and stat
134 // datas - 0xffff, for indirects - 0
135 put_ih_entry_count(ih, entry_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138//
139// FIXME: we might cache recently accessed indirect item
140
141// Ugh. Not too eager for that....
142// I cut the code until such time as I see a convincing argument (benchmark).
143// I don't want a bloated inode struct..., and I don't like code complexity....
144
145/* cutting the code is fine, since it really isn't in use yet and is easy
146** to add back in. But, Vladimir has a really good idea here. Think
147** about what happens for reading a file. For each page,
148** The VFS layer calls reiserfs_readpage, who searches the tree to find
149** an indirect item. This indirect item has X number of pointers, where
150** X is a big number if we've done the block allocation right. But,
151** we only use one or two of these pointers during each call to readpage,
152** needlessly researching again later on.
153**
154** The size of the cache could be dynamic based on the size of the file.
155**
156** I'd also like to see us cache the location the stat data item, since
157** we are needlessly researching for that frequently.
158**
159** --chris
160*/
161
162/* If this page has a file tail in it, and
163** it was read in by get_block_create_0, the page data is valid,
164** but tail is still sitting in a direct item, and we can't write to
165** it. So, look through this page, and check all the mapped buffers
166** to make sure they have valid block numbers. Any that don't need
Christoph Hellwigebdec242010-10-06 10:47:23 +0200167** to be unmapped, so that __block_write_begin will correctly call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168** reiserfs_get_block to convert the tail into an unformatted node
169*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700170static inline void fix_tail_page_for_writing(struct page *page)
171{
172 struct buffer_head *head, *next, *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700174 if (page && page_has_buffers(page)) {
175 head = page_buffers(page);
176 bh = head;
177 do {
178 next = bh->b_this_page;
179 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
180 reiserfs_unmap_buffer(bh);
181 }
182 bh = next;
183 } while (bh != head);
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/* reiserfs_get_block does not need to allocate a block only if it has been
188 done already or non-hole position has been found in the indirect item */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700189static inline int allocation_needed(int retval, b_blocknr_t allocated,
190 struct item_head *ih,
191 __le32 * item, int pos_in_item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700193 if (allocated)
194 return 0;
195 if (retval == POSITION_FOUND && is_indirect_le_ih(ih) &&
196 get_block_num(item, pos_in_item))
197 return 0;
198 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700201static inline int indirect_item_found(int retval, struct item_head *ih)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700203 return (retval == POSITION_FOUND) && is_indirect_le_ih(ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700206static inline void set_block_dev_mapped(struct buffer_head *bh,
207 b_blocknr_t block, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 map_bh(bh, inode->i_sb, block);
210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212//
213// files which were created in the earlier version can not be longer,
214// than 2 gb
215//
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700216static int file_capable(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700218 if (get_inode_item_key_version(inode) != KEY_FORMAT_3_5 || // it is new file.
219 block < (1 << (31 - inode->i_sb->s_blocksize_bits))) // old file, but 'block' is inside of 2gb
220 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Adrian Bunkdeba0f42007-10-16 23:26:03 -0700225static int restart_transaction(struct reiserfs_transaction_handle *th,
226 struct inode *inode, struct treepath *path)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700227{
228 struct super_block *s = th->t_super;
229 int len = th->t_blocks_allocated;
230 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700232 BUG_ON(!th->t_trans_id);
233 BUG_ON(!th->t_refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Suzuki K P87b41262006-12-06 20:36:10 -0800235 pathrelse(path);
236
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700237 /* we cannot restart while nested */
238 if (th->t_refcount > 1) {
239 return 0;
240 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241 reiserfs_update_sd(th, inode);
242 err = journal_end(th, s, len);
243 if (!err) {
244 err = journal_begin(th, s, JOURNAL_PER_BALANCE_CNT * 6);
245 if (!err)
246 reiserfs_update_inode_transaction(inode);
247 }
248 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251// it is called by get_block when create == 0. Returns block number
252// for 'block'-th logical block of file. When it hits direct item it
253// returns 0 (being called from bmap) or read direct item into piece
254// of page (bh_result)
255
256// Please improve the english/clarity in the comment above, as it is
257// hard to understand.
258
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700259static int _get_block_create_0(struct inode *inode, sector_t block,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700260 struct buffer_head *bh_result, int args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700262 INITIALIZE_PATH(path);
263 struct cpu_key key;
264 struct buffer_head *bh;
265 struct item_head *ih, tmp_ih;
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700266 b_blocknr_t blocknr;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700267 char *p = NULL;
268 int chars;
269 int ret;
270 int result;
271 int done = 0;
272 unsigned long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700274 // prepare the key to look for the 'block'-th block of file
275 make_cpu_key(&key, inode,
276 (loff_t) block * inode->i_sb->s_blocksize + 1, TYPE_ANY,
277 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700279 result = search_for_position_by_key(inode->i_sb, &key, &path);
280 if (result != POSITION_FOUND) {
281 pathrelse(&path);
282 if (p)
283 kunmap(bh_result->b_page);
284 if (result == IO_ERROR)
285 return -EIO;
286 // We do not return -ENOENT if there is a hole but page is uptodate, because it means
287 // That there is some MMAPED data associated with it that is yet to be written to disk.
288 if ((args & GET_BLOCK_NO_HOLE)
289 && !PageUptodate(bh_result->b_page)) {
290 return -ENOENT;
291 }
292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700294 //
295 bh = get_last_bh(&path);
296 ih = get_ih(&path);
297 if (is_indirect_le_ih(ih)) {
298 __le32 *ind_item = (__le32 *) B_I_PITEM(bh, ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700300 /* FIXME: here we could cache indirect item or part of it in
301 the inode to avoid search_by_key in case of subsequent
302 access to file */
303 blocknr = get_block_num(ind_item, path.pos_in_item);
304 ret = 0;
305 if (blocknr) {
306 map_bh(bh_result, inode->i_sb, blocknr);
307 if (path.pos_in_item ==
308 ((ih_item_len(ih) / UNFM_P_SIZE) - 1)) {
309 set_buffer_boundary(bh_result);
310 }
311 } else
312 // We do not return -ENOENT if there is a hole but page is uptodate, because it means
313 // That there is some MMAPED data associated with it that is yet to be written to disk.
314 if ((args & GET_BLOCK_NO_HOLE)
315 && !PageUptodate(bh_result->b_page)) {
316 ret = -ENOENT;
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700319 pathrelse(&path);
320 if (p)
321 kunmap(bh_result->b_page);
322 return ret;
323 }
324 // requested data are in direct item(s)
325 if (!(args & GET_BLOCK_READ_DIRECT)) {
326 // we are called by bmap. FIXME: we can not map block of file
327 // when it is stored in direct item(s)
328 pathrelse(&path);
329 if (p)
330 kunmap(bh_result->b_page);
331 return -ENOENT;
332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700334 /* if we've got a direct item, and the buffer or page was uptodate,
335 ** we don't want to pull data off disk again. skip to the
336 ** end, where we map the buffer and return
337 */
338 if (buffer_uptodate(bh_result)) {
339 goto finished;
340 } else
341 /*
342 ** grab_tail_page can trigger calls to reiserfs_get_block on up to date
343 ** pages without any buffers. If the page is up to date, we don't want
344 ** read old data off disk. Set the up to date bit on the buffer instead
345 ** and jump to the end
346 */
347 if (!bh_result->b_page || PageUptodate(bh_result->b_page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 set_buffer_uptodate(bh_result);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700349 goto finished;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700351 // read file tail into part of page
352 offset = (cpu_key_k_offset(&key) - 1) & (PAGE_CACHE_SIZE - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700353 copy_item_head(&tmp_ih, ih);
354
355 /* we only want to kmap if we are reading the tail into the page.
356 ** this is not the common case, so we don't kmap until we are
357 ** sure we need to. But, this means the item might move if
358 ** kmap schedules
359 */
Frederic Weisbecker27b3a5c2009-10-14 23:34:31 +0200360 if (!p)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700361 p = (char *)kmap(bh_result->b_page);
Frederic Weisbecker27b3a5c2009-10-14 23:34:31 +0200362
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700363 p += offset;
364 memset(p, 0, inode->i_sb->s_blocksize);
365 do {
366 if (!is_direct_le_ih(ih)) {
367 BUG();
368 }
369 /* make sure we don't read more bytes than actually exist in
370 ** the file. This can happen in odd cases where i_size isn't
Jeff Mahoney0222e652009-03-30 14:02:44 -0400371 ** correct, and when direct item padding results in a few
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700372 ** extra bytes at the end of the direct item
373 */
374 if ((le_ih_k_offset(ih) + path.pos_in_item) > inode->i_size)
375 break;
376 if ((le_ih_k_offset(ih) - 1 + ih_item_len(ih)) > inode->i_size) {
377 chars =
378 inode->i_size - (le_ih_k_offset(ih) - 1) -
379 path.pos_in_item;
380 done = 1;
381 } else {
382 chars = ih_item_len(ih) - path.pos_in_item;
383 }
384 memcpy(p, B_I_PITEM(bh, ih) + path.pos_in_item, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700386 if (done)
387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700389 p += chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700391 if (PATH_LAST_POSITION(&path) != (B_NR_ITEMS(bh) - 1))
392 // we done, if read direct item is not the last item of
393 // node FIXME: we could try to check right delimiting key
394 // to see whether direct item continues in the right
395 // neighbor or rely on i_size
396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700398 // update key to look for the next piece
399 set_cpu_key_k_offset(&key, cpu_key_k_offset(&key) + chars);
400 result = search_for_position_by_key(inode->i_sb, &key, &path);
401 if (result != POSITION_FOUND)
402 // i/o error most likely
403 break;
404 bh = get_last_bh(&path);
405 ih = get_ih(&path);
406 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700408 flush_dcache_page(bh_result->b_page);
409 kunmap(bh_result->b_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700411 finished:
412 pathrelse(&path);
Qu Fuping6283d582005-06-25 14:55:44 -0700413
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700414 if (result == IO_ERROR)
415 return -EIO;
Qu Fuping6283d582005-06-25 14:55:44 -0700416
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700417 /* this buffer has valid data, but isn't valid for io. mapping it to
418 * block #0 tells the rest of reiserfs it just has a tail in it
419 */
420 map_bh(bh_result, inode->i_sb, 0);
421 set_buffer_uptodate(bh_result);
422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425// this is called to create file map. So, _get_block_create_0 will not
426// read direct item
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700427static int reiserfs_bmap(struct inode *inode, sector_t block,
428 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700430 if (!file_capable(inode, block))
431 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700433 reiserfs_write_lock(inode->i_sb);
434 /* do not read the direct item */
435 _get_block_create_0(inode, block, bh_result, 0);
436 reiserfs_write_unlock(inode->i_sb);
437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440/* special version of get_block that is only used by grab_tail_page right
Christoph Hellwigebdec242010-10-06 10:47:23 +0200441** now. It is sent to __block_write_begin, and when you try to get a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442** block past the end of the file (or a block from a hole) it returns
Christoph Hellwigebdec242010-10-06 10:47:23 +0200443** -ENOENT instead of a valid buffer. __block_write_begin expects to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444** be able to do i/o on the buffers returned, unless an error value
445** is also returned.
Jeff Mahoney0222e652009-03-30 14:02:44 -0400446**
Christoph Hellwigebdec242010-10-06 10:47:23 +0200447** So, this allows __block_write_begin to be used for reading a single block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448** in a page. Where it does not produce a valid page for holes, or past the
449** end of the file. This turns out to be exactly what we need for reading
450** tails for conversion.
451**
452** The point of the wrapper is forcing a certain value for create, even
Jeff Mahoney0222e652009-03-30 14:02:44 -0400453** though the VFS layer is calling this function with create==1. If you
454** don't want to send create == GET_BLOCK_NO_HOLE to reiserfs_get_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455** don't use this function.
456*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700457static int reiserfs_get_block_create_0(struct inode *inode, sector_t block,
458 struct buffer_head *bh_result,
459 int create)
460{
461 return reiserfs_get_block(inode, block, bh_result, GET_BLOCK_NO_HOLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464/* This is special helper for reiserfs_get_block in case we are executing
465 direct_IO request. */
466static int reiserfs_get_blocks_direct_io(struct inode *inode,
467 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct buffer_head *bh_result,
469 int create)
470{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700471 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700473 bh_result->b_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700475 /* We set the b_size before reiserfs_get_block call since it is
476 referenced in convert_tail_for_hole() that may be called from
477 reiserfs_get_block() */
478 bh_result->b_size = (1 << inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700480 ret = reiserfs_get_block(inode, iblock, bh_result,
481 create | GET_BLOCK_NO_DANGLE);
482 if (ret)
483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700485 /* don't allow direct io onto tail pages */
486 if (buffer_mapped(bh_result) && bh_result->b_blocknr == 0) {
487 /* make sure future calls to the direct io funcs for this offset
488 ** in the file fail by unmapping the buffer
489 */
490 clear_buffer_mapped(bh_result);
491 ret = -EINVAL;
492 }
493 /* Possible unpacked tail. Flush the data before pages have
494 disappeared */
495 if (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) {
496 int err;
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200497
498 reiserfs_write_lock(inode->i_sb);
499
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700500 err = reiserfs_commit_for_inode(inode);
501 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200502
503 reiserfs_write_unlock(inode->i_sb);
504
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700505 if (err < 0)
506 ret = err;
507 }
508 out:
509 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/*
513** helper function for when reiserfs_get_block is called for a hole
514** but the file tail is still in a direct item
515** bh_result is the buffer head for the hole
516** tail_offset is the offset of the start of the tail in the file
517**
518** This calls prepare_write, which will start a new transaction
519** you should not be in a transaction, or have any paths held when you
520** call this.
521*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700522static int convert_tail_for_hole(struct inode *inode,
523 struct buffer_head *bh_result,
524 loff_t tail_offset)
525{
526 unsigned long index;
527 unsigned long tail_end;
528 unsigned long tail_start;
529 struct page *tail_page;
530 struct page *hole_page = bh_result->b_page;
531 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700533 if ((tail_offset & (bh_result->b_size - 1)) != 1)
534 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700536 /* always try to read until the end of the block */
537 tail_start = tail_offset & (PAGE_CACHE_SIZE - 1);
538 tail_end = (tail_start | (bh_result->b_size - 1)) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700540 index = tail_offset >> PAGE_CACHE_SHIFT;
541 /* hole_page can be zero in case of direct_io, we are sure
542 that we cannot get here if we write with O_DIRECT into
543 tail page */
544 if (!hole_page || index != hole_page->index) {
545 tail_page = grab_cache_page(inode->i_mapping, index);
546 retval = -ENOMEM;
547 if (!tail_page) {
548 goto out;
549 }
550 } else {
551 tail_page = hole_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700554 /* we don't have to make sure the conversion did not happen while
555 ** we were locking the page because anyone that could convert
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800556 ** must first take i_mutex.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700557 **
558 ** We must fix the tail page for writing because it might have buffers
559 ** that are mapped, but have a block number of 0. This indicates tail
Christoph Hellwigebdec242010-10-06 10:47:23 +0200560 ** data that has been read directly into the page, and
561 ** __block_write_begin won't trigger a get_block in this case.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700562 */
563 fix_tail_page_for_writing(tail_page);
Christoph Hellwigebdec242010-10-06 10:47:23 +0200564 retval = __reiserfs_write_begin(tail_page, tail_start,
565 tail_end - tail_start);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700566 if (retval)
567 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700569 /* tail conversion might change the data in the page */
570 flush_dcache_page(tail_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700572 retval = reiserfs_commit_write(NULL, tail_page, tail_start, tail_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700574 unlock:
575 if (tail_page != hole_page) {
576 unlock_page(tail_page);
577 page_cache_release(tail_page);
578 }
579 out:
580 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583static inline int _allocate_block(struct reiserfs_transaction_handle *th,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700584 sector_t block,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700585 struct inode *inode,
586 b_blocknr_t * allocated_block_nr,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800587 struct treepath *path, int flags)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700588{
589 BUG_ON(!th->t_trans_id);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#ifdef REISERFS_PREALLOCATE
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800592 if (!(flags & GET_BLOCK_NO_IMUX)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700593 return reiserfs_new_unf_blocknrs2(th, inode, allocated_block_nr,
594 path, block);
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700597 return reiserfs_new_unf_blocknrs(th, inode, allocated_block_nr, path,
598 block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700601int reiserfs_get_block(struct inode *inode, sector_t block,
602 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700604 int repeat, retval = 0;
605 b_blocknr_t allocated_block_nr = 0; // b_blocknr_t is (unsigned) 32 bit int
606 INITIALIZE_PATH(path);
607 int pos_in_item;
608 struct cpu_key key;
609 struct buffer_head *bh, *unbh = NULL;
610 struct item_head *ih, tmp_ih;
611 __le32 *item;
612 int done;
613 int fs_gen;
Frederic Weisbecker269313092009-05-07 23:48:44 +0200614 int lock_depth;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700615 struct reiserfs_transaction_handle *th = NULL;
Jeff Mahoney0222e652009-03-30 14:02:44 -0400616 /* space reserved in transaction batch:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617 . 3 balancings in direct->indirect conversion
618 . 1 block involved into reiserfs_update_sd()
619 XXX in practically impossible worst case direct2indirect()
620 can incur (much) more than 3 balancings.
621 quota update for user, group */
622 int jbegin_count =
623 JOURNAL_PER_BALANCE_CNT * 3 + 1 +
624 2 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
625 int version;
626 int dangle = 1;
627 loff_t new_offset =
628 (((loff_t) block) << inode->i_sb->s_blocksize_bits) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Frederic Weisbecker269313092009-05-07 23:48:44 +0200630 lock_depth = reiserfs_write_lock_once(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700631 version = get_inode_item_key_version(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700633 if (!file_capable(inode, block)) {
Frederic Weisbecker269313092009-05-07 23:48:44 +0200634 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700635 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700638 /* if !create, we aren't changing the FS, so we don't need to
639 ** log anything, so we don't need to start a transaction
640 */
641 if (!(create & GET_BLOCK_CREATE)) {
642 int ret;
643 /* find number of block-th logical block of the file */
644 ret = _get_block_create_0(inode, block, bh_result,
645 create | GET_BLOCK_READ_DIRECT);
Frederic Weisbecker269313092009-05-07 23:48:44 +0200646 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700647 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700649 /*
650 * if we're already in a transaction, make sure to close
651 * any new transactions we start in this func
652 */
653 if ((create & GET_BLOCK_NO_DANGLE) ||
654 reiserfs_transaction_running(inode->i_sb))
655 dangle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700657 /* If file is of such a size, that it might have a tail and tails are enabled
658 ** we should mark it as possibly needing tail packing on close
659 */
660 if ((have_large_tails(inode->i_sb)
661 && inode->i_size < i_block_size(inode) * 4)
662 || (have_small_tails(inode->i_sb)
663 && inode->i_size < i_block_size(inode)))
664 REISERFS_I(inode)->i_flags |= i_pack_on_close_mask;
665
666 /* set the key of the first byte in the 'block'-th block of file */
667 make_cpu_key(&key, inode, new_offset, TYPE_ANY, 3 /*key length */ );
668 if ((new_offset + inode->i_sb->s_blocksize - 1) > inode->i_size) {
669 start_trans:
670 th = reiserfs_persistent_transaction(inode->i_sb, jbegin_count);
671 if (!th) {
672 retval = -ENOMEM;
673 goto failure;
674 }
675 reiserfs_update_inode_transaction(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700677 research:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700679 retval = search_for_position_by_key(inode->i_sb, &key, &path);
680 if (retval == IO_ERROR) {
681 retval = -EIO;
682 goto failure;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700685 bh = get_last_bh(&path);
686 ih = get_ih(&path);
687 item = get_item(&path);
688 pos_in_item = path.pos_in_item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700690 fs_gen = get_generation(inode->i_sb);
691 copy_item_head(&tmp_ih, ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700693 if (allocation_needed
694 (retval, allocated_block_nr, ih, item, pos_in_item)) {
695 /* we have to allocate block for the unformatted node */
696 if (!th) {
697 pathrelse(&path);
698 goto start_trans;
699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700701 repeat =
702 _allocate_block(th, block, inode, &allocated_block_nr,
703 &path, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700705 if (repeat == NO_DISK_SPACE || repeat == QUOTA_EXCEEDED) {
706 /* restart the transaction to give the journal a chance to free
707 ** some blocks. releases the path, so we have to go back to
708 ** research if we succeed on the second try
709 */
710 SB_JOURNAL(inode->i_sb)->j_next_async_flush = 1;
711 retval = restart_transaction(th, inode, &path);
712 if (retval)
713 goto failure;
714 repeat =
715 _allocate_block(th, block, inode,
716 &allocated_block_nr, NULL, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700718 if (repeat != NO_DISK_SPACE && repeat != QUOTA_EXCEEDED) {
719 goto research;
720 }
721 if (repeat == QUOTA_EXCEEDED)
722 retval = -EDQUOT;
723 else
724 retval = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto failure;
726 }
727
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700728 if (fs_changed(fs_gen, inode->i_sb)
729 && item_moved(&tmp_ih, &path)) {
730 goto research;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700734 if (indirect_item_found(retval, ih)) {
735 b_blocknr_t unfm_ptr;
736 /* 'block'-th block is in the file already (there is
737 corresponding cell in some indirect item). But it may be
738 zero unformatted node pointer (hole) */
739 unfm_ptr = get_block_num(item, pos_in_item);
740 if (unfm_ptr == 0) {
741 /* use allocated block to plug the hole */
742 reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
743 if (fs_changed(fs_gen, inode->i_sb)
744 && item_moved(&tmp_ih, &path)) {
745 reiserfs_restore_prepared_buffer(inode->i_sb,
746 bh);
747 goto research;
748 }
749 set_buffer_new(bh_result);
750 if (buffer_dirty(bh_result)
751 && reiserfs_data_ordered(inode->i_sb))
752 reiserfs_add_ordered_list(inode, bh_result);
753 put_block_num(item, pos_in_item, allocated_block_nr);
754 unfm_ptr = allocated_block_nr;
755 journal_mark_dirty(th, inode->i_sb, bh);
756 reiserfs_update_sd(th, inode);
757 }
758 set_block_dev_mapped(bh_result, unfm_ptr, inode);
759 pathrelse(&path);
760 retval = 0;
761 if (!dangle && th)
762 retval = reiserfs_end_persistent_transaction(th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Frederic Weisbecker269313092009-05-07 23:48:44 +0200764 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700765
766 /* the item was found, so new blocks were not added to the file
Jeff Mahoney0222e652009-03-30 14:02:44 -0400767 ** there is no need to make sure the inode is updated with this
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700768 ** transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700773 if (!th) {
774 pathrelse(&path);
775 goto start_trans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700778 /* desired position is not found or is in the direct item. We have
779 to append file with holes up to 'block'-th block converting
780 direct items to indirect one if necessary */
781 done = 0;
782 do {
783 if (is_statdata_le_ih(ih)) {
784 __le32 unp = 0;
785 struct cpu_key tmp_key;
786
787 /* indirect item has to be inserted */
788 make_le_item_head(&tmp_ih, &key, version, 1,
789 TYPE_INDIRECT, UNFM_P_SIZE,
790 0 /* free_space */ );
791
792 if (cpu_key_k_offset(&key) == 1) {
793 /* we are going to add 'block'-th block to the file. Use
794 allocated block for that */
795 unp = cpu_to_le32(allocated_block_nr);
796 set_block_dev_mapped(bh_result,
797 allocated_block_nr, inode);
798 set_buffer_new(bh_result);
799 done = 1;
800 }
801 tmp_key = key; // ;)
802 set_cpu_key_k_offset(&tmp_key, 1);
803 PATH_LAST_POSITION(&path)++;
804
805 retval =
806 reiserfs_insert_item(th, &path, &tmp_key, &tmp_ih,
807 inode, (char *)&unp);
808 if (retval) {
809 reiserfs_free_block(th, inode,
810 allocated_block_nr, 1);
811 goto failure; // retval == -ENOSPC, -EDQUOT or -EIO or -EEXIST
812 }
813 //mark_tail_converted (inode);
814 } else if (is_direct_le_ih(ih)) {
815 /* direct item has to be converted */
816 loff_t tail_offset;
817
818 tail_offset =
819 ((le_ih_k_offset(ih) -
820 1) & ~(inode->i_sb->s_blocksize - 1)) + 1;
821 if (tail_offset == cpu_key_k_offset(&key)) {
822 /* direct item we just found fits into block we have
823 to map. Convert it into unformatted node: use
824 bh_result for the conversion */
825 set_block_dev_mapped(bh_result,
826 allocated_block_nr, inode);
827 unbh = bh_result;
828 done = 1;
829 } else {
830 /* we have to padd file tail stored in direct item(s)
831 up to block size and convert it to unformatted
832 node. FIXME: this should also get into page cache */
833
834 pathrelse(&path);
835 /*
836 * ugly, but we can only end the transaction if
837 * we aren't nested
838 */
839 BUG_ON(!th->t_refcount);
840 if (th->t_refcount == 1) {
841 retval =
842 reiserfs_end_persistent_transaction
843 (th);
844 th = NULL;
845 if (retval)
846 goto failure;
847 }
848
849 retval =
850 convert_tail_for_hole(inode, bh_result,
851 tail_offset);
852 if (retval) {
853 if (retval != -ENOSPC)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400854 reiserfs_error(inode->i_sb,
855 "clm-6004",
856 "convert tail failed "
857 "inode %lu, error %d",
858 inode->i_ino,
859 retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700860 if (allocated_block_nr) {
861 /* the bitmap, the super, and the stat data == 3 */
862 if (!th)
863 th = reiserfs_persistent_transaction(inode->i_sb, 3);
864 if (th)
865 reiserfs_free_block(th,
866 inode,
867 allocated_block_nr,
868 1);
869 }
870 goto failure;
871 }
872 goto research;
873 }
874 retval =
875 direct2indirect(th, inode, &path, unbh,
876 tail_offset);
877 if (retval) {
878 reiserfs_unmap_buffer(unbh);
879 reiserfs_free_block(th, inode,
880 allocated_block_nr, 1);
881 goto failure;
882 }
883 /* it is important the set_buffer_uptodate is done after
884 ** the direct2indirect. The buffer might contain valid
885 ** data newer than the data on disk (read by readpage, changed,
886 ** and then sent here by writepage). direct2indirect needs
887 ** to know if unbh was already up to date, so it can decide
888 ** if the data in unbh needs to be replaced with data from
889 ** the disk
890 */
891 set_buffer_uptodate(unbh);
892
893 /* unbh->b_page == NULL in case of DIRECT_IO request, this means
894 buffer will disappear shortly, so it should not be added to
895 */
896 if (unbh->b_page) {
897 /* we've converted the tail, so we must
898 ** flush unbh before the transaction commits
899 */
900 reiserfs_add_tail_list(inode, unbh);
901
902 /* mark it dirty now to prevent commit_write from adding
903 ** this buffer to the inode's dirty buffer list
904 */
905 /*
906 * AKPM: changed __mark_buffer_dirty to mark_buffer_dirty().
907 * It's still atomic, but it sets the page dirty too,
908 * which makes it eligible for writeback at any time by the
909 * VM (which was also the case with __mark_buffer_dirty())
910 */
911 mark_buffer_dirty(unbh);
912 }
913 } else {
914 /* append indirect item with holes if needed, when appending
915 pointer to 'block'-th block use block, which is already
916 allocated */
917 struct cpu_key tmp_key;
918 unp_t unf_single = 0; // We use this in case we need to allocate only
919 // one block which is a fastpath
920 unp_t *un;
921 __u64 max_to_insert =
922 MAX_ITEM_LEN(inode->i_sb->s_blocksize) /
923 UNFM_P_SIZE;
924 __u64 blocks_needed;
925
926 RFALSE(pos_in_item != ih_item_len(ih) / UNFM_P_SIZE,
927 "vs-804: invalid position for append");
928 /* indirect item has to be appended, set up key of that position */
929 make_cpu_key(&tmp_key, inode,
930 le_key_k_offset(version,
931 &(ih->ih_key)) +
932 op_bytes_number(ih,
933 inode->i_sb->s_blocksize),
934 //pos_in_item * inode->i_sb->s_blocksize,
935 TYPE_INDIRECT, 3); // key type is unimportant
936
Vladimir V. Savelievc499ec22006-03-02 02:54:39 -0800937 RFALSE(cpu_key_k_offset(&tmp_key) > cpu_key_k_offset(&key),
938 "green-805: invalid offset");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700939 blocks_needed =
940 1 +
941 ((cpu_key_k_offset(&key) -
942 cpu_key_k_offset(&tmp_key)) >> inode->i_sb->
943 s_blocksize_bits);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700944
945 if (blocks_needed == 1) {
946 un = &unf_single;
947 } else {
Frederic Weisbecker1d2c6cf2009-11-20 04:11:30 +0100948 un = kzalloc(min(blocks_needed, max_to_insert) * UNFM_P_SIZE, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700949 if (!un) {
950 un = &unf_single;
951 blocks_needed = 1;
952 max_to_insert = 0;
Yan Burman01afb212006-12-06 20:39:01 -0800953 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700954 }
955 if (blocks_needed <= max_to_insert) {
956 /* we are going to add target block to the file. Use allocated
957 block for that */
958 un[blocks_needed - 1] =
959 cpu_to_le32(allocated_block_nr);
960 set_block_dev_mapped(bh_result,
961 allocated_block_nr, inode);
962 set_buffer_new(bh_result);
963 done = 1;
964 } else {
965 /* paste hole to the indirect item */
966 /* If kmalloc failed, max_to_insert becomes zero and it means we
967 only have space for one block */
968 blocks_needed =
969 max_to_insert ? max_to_insert : 1;
970 }
971 retval =
972 reiserfs_paste_into_item(th, &path, &tmp_key, inode,
973 (char *)un,
974 UNFM_P_SIZE *
975 blocks_needed);
976
977 if (blocks_needed != 1)
978 kfree(un);
979
980 if (retval) {
981 reiserfs_free_block(th, inode,
982 allocated_block_nr, 1);
983 goto failure;
984 }
985 if (!done) {
986 /* We need to mark new file size in case this function will be
987 interrupted/aborted later on. And we may do this only for
988 holes. */
989 inode->i_size +=
990 inode->i_sb->s_blocksize * blocks_needed;
991 }
992 }
993
994 if (done == 1)
995 break;
996
997 /* this loop could log more blocks than we had originally asked
998 ** for. So, we have to allow the transaction to end if it is
Jeff Mahoney0222e652009-03-30 14:02:44 -0400999 ** too big or too full. Update the inode so things are
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001000 ** consistent if we crash before the function returns
1001 **
1002 ** release the path so that anybody waiting on the path before
1003 ** ending their transaction will be able to continue.
1004 */
1005 if (journal_transaction_should_end(th, th->t_blocks_allocated)) {
1006 retval = restart_transaction(th, inode, &path);
1007 if (retval)
1008 goto failure;
1009 }
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001010 /*
1011 * inserting indirect pointers for a hole can take a
1012 * long time. reschedule if needed and also release the write
1013 * lock for others.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001014 */
Frederic Weisbecker269313092009-05-07 23:48:44 +02001015 if (need_resched()) {
1016 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
1017 schedule();
1018 lock_depth = reiserfs_write_lock_once(inode->i_sb);
1019 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001020
1021 retval = search_for_position_by_key(inode->i_sb, &key, &path);
1022 if (retval == IO_ERROR) {
1023 retval = -EIO;
1024 goto failure;
1025 }
1026 if (retval == POSITION_FOUND) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001027 reiserfs_warning(inode->i_sb, "vs-825",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001028 "%K should not be found", &key);
1029 retval = -EEXIST;
1030 if (allocated_block_nr)
1031 reiserfs_free_block(th, inode,
1032 allocated_block_nr, 1);
1033 pathrelse(&path);
1034 goto failure;
1035 }
1036 bh = get_last_bh(&path);
1037 ih = get_ih(&path);
1038 item = get_item(&path);
1039 pos_in_item = path.pos_in_item;
1040 } while (1);
1041
1042 retval = 0;
1043
1044 failure:
1045 if (th && (!dangle || (retval && !th->t_trans_id))) {
1046 int err;
1047 if (th->t_trans_id)
1048 reiserfs_update_sd(th, inode);
1049 err = reiserfs_end_persistent_transaction(th);
1050 if (err)
1051 retval = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Frederic Weisbecker269313092009-05-07 23:48:44 +02001054 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001055 reiserfs_check_path(&path);
1056 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059static int
1060reiserfs_readpages(struct file *file, struct address_space *mapping,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001061 struct list_head *pages, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001063 return mpage_readpages(mapping, pages, nr_pages, reiserfs_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066/* Compute real number of used bytes by file
1067 * Following three functions can go away when we'll have enough space in stat item
1068 */
1069static int real_space_diff(struct inode *inode, int sd_size)
1070{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001071 int bytes;
1072 loff_t blocksize = inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001074 if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode))
1075 return sd_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001077 /* End of file is also in full block with indirect reference, so round
1078 ** up to the next block.
1079 **
1080 ** there is just no way to know if the tail is actually packed
1081 ** on the file, so we have to assume it isn't. When we pack the
1082 ** tail, we add 4 bytes to pretend there really is an unformatted
1083 ** node pointer
1084 */
1085 bytes =
1086 ((inode->i_size +
1087 (blocksize - 1)) >> inode->i_sb->s_blocksize_bits) * UNFM_P_SIZE +
1088 sd_size;
1089 return bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092static inline loff_t to_real_used_space(struct inode *inode, ulong blocks,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001093 int sd_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001095 if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode)) {
1096 return inode->i_size +
1097 (loff_t) (real_space_diff(inode, sd_size));
1098 }
1099 return ((loff_t) real_space_diff(inode, sd_size)) +
1100 (((loff_t) blocks) << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
1103/* Compute number of blocks used by file in ReiserFS counting */
1104static inline ulong to_fake_used_blocks(struct inode *inode, int sd_size)
1105{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001106 loff_t bytes = inode_get_bytes(inode);
1107 loff_t real_space = real_space_diff(inode, sd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001109 /* keeps fsck and non-quota versions of reiserfs happy */
1110 if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode)) {
1111 bytes += (loff_t) 511;
1112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001114 /* files from before the quota patch might i_blocks such that
1115 ** bytes < real_space. Deal with that here to prevent it from
1116 ** going negative.
1117 */
1118 if (bytes < real_space)
1119 return 0;
1120 return (bytes - real_space) >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123//
1124// BAD: new directories have stat data of new type and all other items
1125// of old type. Version stored in the inode says about body items, so
1126// in update_stat_data we can not rely on inode, but have to check
1127// item version directly
1128//
1129
1130// called by read_locked_inode
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -08001131static void init_inode(struct inode *inode, struct treepath *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001133 struct buffer_head *bh;
1134 struct item_head *ih;
1135 __u32 rdev;
1136 //int version = ITEM_VERSION_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001138 bh = PATH_PLAST_BUFFER(path);
1139 ih = PATH_PITEM_HEAD(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001141 copy_key(INODE_PKEY(inode), &(ih->ih_key));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001143 INIT_LIST_HEAD(&(REISERFS_I(inode)->i_prealloc_list));
1144 REISERFS_I(inode)->i_flags = 0;
1145 REISERFS_I(inode)->i_prealloc_block = 0;
1146 REISERFS_I(inode)->i_prealloc_count = 0;
1147 REISERFS_I(inode)->i_trans_id = 0;
1148 REISERFS_I(inode)->i_jl = NULL;
Alexey Dobriyan068fbb32006-09-29 01:59:58 -07001149 reiserfs_init_xattr_rwsem(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001151 if (stat_data_v1(ih)) {
1152 struct stat_data_v1 *sd =
1153 (struct stat_data_v1 *)B_I_PITEM(bh, ih);
1154 unsigned long blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001156 set_inode_item_key_version(inode, KEY_FORMAT_3_5);
1157 set_inode_sd_version(inode, STAT_DATA_V1);
1158 inode->i_mode = sd_v1_mode(sd);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001159 set_nlink(inode, sd_v1_nlink(sd));
Eric W. Biedermandf814652012-02-07 16:30:06 -08001160 i_uid_write(inode, sd_v1_uid(sd));
1161 i_gid_write(inode, sd_v1_gid(sd));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001162 inode->i_size = sd_v1_size(sd);
1163 inode->i_atime.tv_sec = sd_v1_atime(sd);
1164 inode->i_mtime.tv_sec = sd_v1_mtime(sd);
1165 inode->i_ctime.tv_sec = sd_v1_ctime(sd);
1166 inode->i_atime.tv_nsec = 0;
1167 inode->i_ctime.tv_nsec = 0;
1168 inode->i_mtime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001170 inode->i_blocks = sd_v1_blocks(sd);
1171 inode->i_generation = le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
1172 blocks = (inode->i_size + 511) >> 9;
1173 blocks = _ROUND_UP(blocks, inode->i_sb->s_blocksize >> 9);
1174 if (inode->i_blocks > blocks) {
1175 // there was a bug in <=3.5.23 when i_blocks could take negative
1176 // values. Starting from 3.5.17 this value could even be stored in
1177 // stat data. For such files we set i_blocks based on file
1178 // size. Just 2 notes: this can be wrong for sparce files. On-disk value will be
1179 // only updated if file's inode will ever change
1180 inode->i_blocks = blocks;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001183 rdev = sd_v1_rdev(sd);
1184 REISERFS_I(inode)->i_first_direct_byte =
1185 sd_v1_first_direct_byte(sd);
1186 /* an early bug in the quota code can give us an odd number for the
1187 ** block count. This is incorrect, fix it here.
1188 */
1189 if (inode->i_blocks & 1) {
1190 inode->i_blocks++;
1191 }
1192 inode_set_bytes(inode,
1193 to_real_used_space(inode, inode->i_blocks,
1194 SD_V1_SIZE));
1195 /* nopack is initially zero for v1 objects. For v2 objects,
1196 nopack is initialised from sd_attrs */
1197 REISERFS_I(inode)->i_flags &= ~i_nopack_mask;
1198 } else {
1199 // new stat data found, but object may have old items
1200 // (directories and symlinks)
1201 struct stat_data *sd = (struct stat_data *)B_I_PITEM(bh, ih);
1202
1203 inode->i_mode = sd_v2_mode(sd);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001204 set_nlink(inode, sd_v2_nlink(sd));
Eric W. Biedermandf814652012-02-07 16:30:06 -08001205 i_uid_write(inode, sd_v2_uid(sd));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001206 inode->i_size = sd_v2_size(sd);
Eric W. Biedermandf814652012-02-07 16:30:06 -08001207 i_gid_write(inode, sd_v2_gid(sd));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001208 inode->i_mtime.tv_sec = sd_v2_mtime(sd);
1209 inode->i_atime.tv_sec = sd_v2_atime(sd);
1210 inode->i_ctime.tv_sec = sd_v2_ctime(sd);
1211 inode->i_ctime.tv_nsec = 0;
1212 inode->i_mtime.tv_nsec = 0;
1213 inode->i_atime.tv_nsec = 0;
1214 inode->i_blocks = sd_v2_blocks(sd);
1215 rdev = sd_v2_rdev(sd);
1216 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1217 inode->i_generation =
1218 le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
1219 else
1220 inode->i_generation = sd_v2_generation(sd);
1221
1222 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1223 set_inode_item_key_version(inode, KEY_FORMAT_3_5);
1224 else
1225 set_inode_item_key_version(inode, KEY_FORMAT_3_6);
1226 REISERFS_I(inode)->i_first_direct_byte = 0;
1227 set_inode_sd_version(inode, STAT_DATA_V2);
1228 inode_set_bytes(inode,
1229 to_real_used_space(inode, inode->i_blocks,
1230 SD_V2_SIZE));
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001231 /* read persistent inode attributes from sd and initialise
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001232 generic inode flags from them */
1233 REISERFS_I(inode)->i_attrs = sd_v2_attrs(sd);
1234 sd_attrs_to_i_attrs(sd_v2_attrs(sd), inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001237 pathrelse(path);
1238 if (S_ISREG(inode->i_mode)) {
1239 inode->i_op = &reiserfs_file_inode_operations;
1240 inode->i_fop = &reiserfs_file_operations;
1241 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1242 } else if (S_ISDIR(inode->i_mode)) {
1243 inode->i_op = &reiserfs_dir_inode_operations;
1244 inode->i_fop = &reiserfs_dir_operations;
1245 } else if (S_ISLNK(inode->i_mode)) {
1246 inode->i_op = &reiserfs_symlink_inode_operations;
1247 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1248 } else {
1249 inode->i_blocks = 0;
1250 inode->i_op = &reiserfs_special_inode_operations;
1251 init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255// update new stat data with inode fields
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001256static void inode2sd(void *sd, struct inode *inode, loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001258 struct stat_data *sd_v2 = (struct stat_data *)sd;
1259 __u16 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001261 set_sd_v2_mode(sd_v2, inode->i_mode);
1262 set_sd_v2_nlink(sd_v2, inode->i_nlink);
Eric W. Biedermandf814652012-02-07 16:30:06 -08001263 set_sd_v2_uid(sd_v2, i_uid_read(inode));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001264 set_sd_v2_size(sd_v2, size);
Eric W. Biedermandf814652012-02-07 16:30:06 -08001265 set_sd_v2_gid(sd_v2, i_gid_read(inode));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001266 set_sd_v2_mtime(sd_v2, inode->i_mtime.tv_sec);
1267 set_sd_v2_atime(sd_v2, inode->i_atime.tv_sec);
1268 set_sd_v2_ctime(sd_v2, inode->i_ctime.tv_sec);
1269 set_sd_v2_blocks(sd_v2, to_fake_used_blocks(inode, SD_V2_SIZE));
1270 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1271 set_sd_v2_rdev(sd_v2, new_encode_dev(inode->i_rdev));
1272 else
1273 set_sd_v2_generation(sd_v2, inode->i_generation);
1274 flags = REISERFS_I(inode)->i_attrs;
1275 i_attrs_to_sd_attrs(inode, &flags);
1276 set_sd_v2_attrs(sd_v2, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279// used to copy inode's fields to old stat data
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001280static void inode2sd_v1(void *sd, struct inode *inode, loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001282 struct stat_data_v1 *sd_v1 = (struct stat_data_v1 *)sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001284 set_sd_v1_mode(sd_v1, inode->i_mode);
Eric W. Biedermandf814652012-02-07 16:30:06 -08001285 set_sd_v1_uid(sd_v1, i_uid_read(inode));
1286 set_sd_v1_gid(sd_v1, i_gid_read(inode));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001287 set_sd_v1_nlink(sd_v1, inode->i_nlink);
1288 set_sd_v1_size(sd_v1, size);
1289 set_sd_v1_atime(sd_v1, inode->i_atime.tv_sec);
1290 set_sd_v1_ctime(sd_v1, inode->i_ctime.tv_sec);
1291 set_sd_v1_mtime(sd_v1, inode->i_mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001293 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1294 set_sd_v1_rdev(sd_v1, new_encode_dev(inode->i_rdev));
1295 else
1296 set_sd_v1_blocks(sd_v1, to_fake_used_blocks(inode, SD_V1_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001298 // Sigh. i_first_direct_byte is back
1299 set_sd_v1_first_direct_byte(sd_v1,
1300 REISERFS_I(inode)->i_first_direct_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303/* NOTE, you must prepare the buffer head before sending it here,
1304** and then log it after the call
1305*/
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -08001306static void update_stat_data(struct treepath *path, struct inode *inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001307 loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001309 struct buffer_head *bh;
1310 struct item_head *ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001312 bh = PATH_PLAST_BUFFER(path);
1313 ih = PATH_PITEM_HEAD(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001315 if (!is_statdata_le_ih(ih))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001316 reiserfs_panic(inode->i_sb, "vs-13065", "key %k, found item %h",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001317 INODE_PKEY(inode), ih);
1318
1319 if (stat_data_v1(ih)) {
1320 // path points to old stat data
1321 inode2sd_v1(B_I_PITEM(bh, ih), inode, size);
1322 } else {
1323 inode2sd(B_I_PITEM(bh, ih), inode, size);
1324 }
1325
1326 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001329void reiserfs_update_sd_size(struct reiserfs_transaction_handle *th,
1330 struct inode *inode, loff_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001332 struct cpu_key key;
1333 INITIALIZE_PATH(path);
1334 struct buffer_head *bh;
1335 int fs_gen;
1336 struct item_head *ih, tmp_ih;
1337 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001339 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001341 make_cpu_key(&key, inode, SD_OFFSET, TYPE_STAT_DATA, 3); //key type is unimportant
1342
1343 for (;;) {
1344 int pos;
1345 /* look for the object's stat data */
1346 retval = search_item(inode->i_sb, &key, &path);
1347 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -04001348 reiserfs_error(inode->i_sb, "vs-13050",
1349 "i/o failure occurred trying to "
1350 "update %K stat data", &key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001351 return;
1352 }
1353 if (retval == ITEM_NOT_FOUND) {
1354 pos = PATH_LAST_POSITION(&path);
1355 pathrelse(&path);
1356 if (inode->i_nlink == 0) {
1357 /*reiserfs_warning (inode->i_sb, "vs-13050: reiserfs_update_sd: i_nlink == 0, stat data not found"); */
1358 return;
1359 }
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001360 reiserfs_warning(inode->i_sb, "vs-13060",
1361 "stat data of object %k (nlink == %d) "
1362 "not found (pos %d)",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001363 INODE_PKEY(inode), inode->i_nlink,
1364 pos);
1365 reiserfs_check_path(&path);
1366 return;
1367 }
1368
1369 /* sigh, prepare_for_journal might schedule. When it schedules the
1370 ** FS might change. We have to detect that, and loop back to the
1371 ** search if the stat data item has moved
1372 */
1373 bh = get_last_bh(&path);
1374 ih = get_ih(&path);
1375 copy_item_head(&tmp_ih, ih);
1376 fs_gen = get_generation(inode->i_sb);
1377 reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
1378 if (fs_changed(fs_gen, inode->i_sb)
1379 && item_moved(&tmp_ih, &path)) {
1380 reiserfs_restore_prepared_buffer(inode->i_sb, bh);
1381 continue; /* Stat_data item has been moved after scheduling. */
1382 }
1383 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001385 update_stat_data(&path, inode, size);
1386 journal_mark_dirty(th, th->t_super, bh);
1387 pathrelse(&path);
1388 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391/* reiserfs_read_locked_inode is called to read the inode off disk, and it
1392** does a make_bad_inode when things go wrong. But, we need to make sure
1393** and clear the key in the private portion of the inode, otherwise a
1394** corresponding iput might try to delete whatever object the inode last
1395** represented.
1396*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001397static void reiserfs_make_bad_inode(struct inode *inode)
1398{
1399 memset(INODE_PKEY(inode), 0, KEY_SIZE);
1400 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403//
1404// initially this function was derived from minix or ext2's analog and
1405// evolved as the prototype did
1406//
1407
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001408int reiserfs_init_locked_inode(struct inode *inode, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001410 struct reiserfs_iget_args *args = (struct reiserfs_iget_args *)p;
1411 inode->i_ino = args->objectid;
1412 INODE_PKEY(inode)->k_dir_id = cpu_to_le32(args->dirid);
1413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416/* looks for stat data in the tree, and fills up the fields of in-core
1417 inode stat data fields */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001418void reiserfs_read_locked_inode(struct inode *inode,
1419 struct reiserfs_iget_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001421 INITIALIZE_PATH(path_to_sd);
1422 struct cpu_key key;
1423 unsigned long dirino;
1424 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001426 dirino = args->dirid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001428 /* set version 1, version 2 could be used too, because stat data
1429 key is the same in both versions */
1430 key.version = KEY_FORMAT_3_5;
1431 key.on_disk_key.k_dir_id = dirino;
1432 key.on_disk_key.k_objectid = inode->i_ino;
1433 key.on_disk_key.k_offset = 0;
1434 key.on_disk_key.k_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001436 /* look for the object's stat data */
1437 retval = search_item(inode->i_sb, &key, &path_to_sd);
1438 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -04001439 reiserfs_error(inode->i_sb, "vs-13070",
1440 "i/o failure occurred trying to find "
1441 "stat data of %K", &key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001442 reiserfs_make_bad_inode(inode);
1443 return;
1444 }
1445 if (retval != ITEM_FOUND) {
1446 /* a stale NFS handle can trigger this without it being an error */
1447 pathrelse(&path_to_sd);
1448 reiserfs_make_bad_inode(inode);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001449 clear_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001450 return;
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001453 init_inode(inode, &path_to_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001455 /* It is possible that knfsd is trying to access inode of a file
1456 that is being removed from the disk by some other thread. As we
1457 update sd on unlink all that is required is to check for nlink
1458 here. This bug was first found by Sizif when debugging
1459 SquidNG/Butterfly, forgotten, and found again after Philippe
Jeff Mahoney0222e652009-03-30 14:02:44 -04001460 Gramoulle <philippe.gramoulle@mmania.com> reproduced it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001462 More logical fix would require changes in fs/inode.c:iput() to
1463 remove inode from hash-table _after_ fs cleaned disk stuff up and
1464 in iget() to return NULL if I_FREEING inode is found in
1465 hash-table. */
1466 /* Currently there is one place where it's ok to meet inode with
1467 nlink==0: processing of open-unlinked and half-truncated files
1468 during mount (fs/reiserfs/super.c:finish_unfinished()). */
1469 if ((inode->i_nlink == 0) &&
1470 !REISERFS_SB(inode->i_sb)->s_is_unlinked_ok) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001471 reiserfs_warning(inode->i_sb, "vs-13075",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001472 "dead inode read from disk %K. "
1473 "This is likely to be race with knfsd. Ignore",
1474 &key);
1475 reiserfs_make_bad_inode(inode);
1476 }
1477
1478 reiserfs_check_path(&path_to_sd); /* init inode should be relsing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Christoph Hellwig4482a082011-07-23 17:37:03 +02001480 /*
1481 * Stat data v1 doesn't support ACLs.
1482 */
1483 if (get_inode_sd_version(inode) == STAT_DATA_V1)
1484 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487/**
1488 * reiserfs_find_actor() - "find actor" reiserfs supplies to iget5_locked().
1489 *
1490 * @inode: inode from hash table to check
1491 * @opaque: "cookie" passed to iget5_locked(). This is &reiserfs_iget_args.
1492 *
1493 * This function is called by iget5_locked() to distinguish reiserfs inodes
1494 * having the same inode numbers. Such inodes can only exist due to some
1495 * error condition. One of them should be bad. Inodes with identical
1496 * inode numbers (objectids) are distinguished by parent directory ids.
1497 *
1498 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001499int reiserfs_find_actor(struct inode *inode, void *opaque)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001501 struct reiserfs_iget_args *args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001503 args = opaque;
1504 /* args is already in CPU order */
1505 return (inode->i_ino == args->objectid) &&
1506 (le32_to_cpu(INODE_PKEY(inode)->k_dir_id) == args->dirid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001509struct inode *reiserfs_iget(struct super_block *s, const struct cpu_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001511 struct inode *inode;
1512 struct reiserfs_iget_args args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001514 args.objectid = key->on_disk_key.k_objectid;
1515 args.dirid = key->on_disk_key.k_dir_id;
Frederic Weisbecker175359f2010-02-11 13:13:10 +01001516 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001517 inode = iget5_locked(s, key->on_disk_key.k_objectid,
1518 reiserfs_find_actor, reiserfs_init_locked_inode,
1519 (void *)(&args));
Frederic Weisbecker175359f2010-02-11 13:13:10 +01001520 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001521 if (!inode)
1522 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001524 if (inode->i_state & I_NEW) {
1525 reiserfs_read_locked_inode(inode, &args);
1526 unlock_new_inode(inode);
1527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001529 if (comp_short_keys(INODE_PKEY(inode), key) || is_bad_inode(inode)) {
1530 /* either due to i/o error or a stale NFS handle */
1531 iput(inode);
1532 inode = NULL;
1533 }
1534 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001537static struct dentry *reiserfs_get_dentry(struct super_block *sb,
1538 u32 objectid, u32 dir_id, u32 generation)
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001541 struct cpu_key key;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001542 struct inode *inode;
1543
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001544 key.on_disk_key.k_objectid = objectid;
1545 key.on_disk_key.k_dir_id = dir_id;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001546 reiserfs_write_lock(sb);
1547 inode = reiserfs_iget(sb, &key);
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001548 if (inode && !IS_ERR(inode) && generation != 0 &&
1549 generation != inode->i_generation) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001550 iput(inode);
1551 inode = NULL;
1552 }
1553 reiserfs_write_unlock(sb);
Christoph Hellwig44003722008-08-11 15:49:04 +02001554
1555 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001558struct dentry *reiserfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1559 int fh_len, int fh_type)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001560{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001561 /* fhtype happens to reflect the number of u32s encoded.
1562 * due to a bug in earlier code, fhtype might indicate there
1563 * are more u32s then actually fitted.
1564 * so if fhtype seems to be more than len, reduce fhtype.
1565 * Valid types are:
1566 * 2 - objectid + dir_id - legacy support
1567 * 3 - objectid + dir_id + generation
1568 * 4 - objectid + dir_id + objectid and dirid of parent - legacy
1569 * 5 - objectid + dir_id + generation + objectid and dirid of parent
1570 * 6 - as above plus generation of directory
1571 * 6 does not fit in NFSv2 handles
1572 */
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001573 if (fh_type > fh_len) {
1574 if (fh_type != 6 || fh_len != 5)
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001575 reiserfs_warning(sb, "reiserfs-13077",
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001576 "nfsd/reiserfs, fhtype=%d, len=%d - odd",
1577 fh_type, fh_len);
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07001578 fh_type = fh_len;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001579 }
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07001580 if (fh_len < 2)
1581 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001583 return reiserfs_get_dentry(sb, fid->raw[0], fid->raw[1],
1584 (fh_type == 3 || fh_type >= 5) ? fid->raw[2] : 0);
1585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001587struct dentry *reiserfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1588 int fh_len, int fh_type)
1589{
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07001590 if (fh_type > fh_len)
1591 fh_type = fh_len;
Christoph Hellwigbe55caf2007-10-21 16:42:13 -07001592 if (fh_type < 4)
1593 return NULL;
1594
1595 return reiserfs_get_dentry(sb,
1596 (fh_type >= 5) ? fid->raw[3] : fid->raw[2],
1597 (fh_type >= 5) ? fid->raw[4] : fid->raw[3],
1598 (fh_type == 6) ? fid->raw[5] : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
Al Virob0b03822012-04-02 14:34:06 -04001601int reiserfs_encode_fh(struct inode *inode, __u32 * data, int *lenp,
1602 struct inode *parent)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001603{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001604 int maxlen = *lenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Al Virob0b03822012-04-02 14:34:06 -04001606 if (parent && (maxlen < 5)) {
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301607 *lenp = 5;
Namjae Jeon94e07a752013-02-17 15:48:11 +09001608 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301609 } else if (maxlen < 3) {
1610 *lenp = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09001611 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001614 data[0] = inode->i_ino;
1615 data[1] = le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
1616 data[2] = inode->i_generation;
1617 *lenp = 3;
Al Virob0b03822012-04-02 14:34:06 -04001618 if (parent) {
1619 data[3] = parent->i_ino;
1620 data[4] = le32_to_cpu(INODE_PKEY(parent)->k_dir_id);
1621 *lenp = 5;
1622 if (maxlen >= 6) {
1623 data[5] = parent->i_generation;
1624 *lenp = 6;
1625 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001626 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001627 return *lenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630/* looks for stat data, then copies fields to it, marks the buffer
1631 containing stat data as dirty */
1632/* reiserfs inodes are never really dirty, since the dirty inode call
1633** always logs them. This call allows the VFS inode marking routines
1634** to properly mark inodes for datasync and such, but only actually
1635** does something when called for a synchronous update.
1636*/
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001637int reiserfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001638{
1639 struct reiserfs_transaction_handle th;
1640 int jbegin_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001642 if (inode->i_sb->s_flags & MS_RDONLY)
1643 return -EROFS;
1644 /* memory pressure can sometimes initiate write_inode calls with sync == 1,
Jeff Mahoney0222e652009-03-30 14:02:44 -04001645 ** these cases are just when the system needs ram, not when the
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001646 ** inode needs to reach disk for safety, and they can safely be
1647 ** ignored because the altered inode has already been logged.
1648 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001649 if (wbc->sync_mode == WB_SYNC_ALL && !(current->flags & PF_MEMALLOC)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001650 reiserfs_write_lock(inode->i_sb);
1651 if (!journal_begin(&th, inode->i_sb, jbegin_count)) {
1652 reiserfs_update_sd(&th, inode);
1653 journal_end_sync(&th, inode->i_sb, jbegin_count);
1654 }
1655 reiserfs_write_unlock(inode->i_sb);
1656 }
1657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
1660/* stat data of new object is inserted already, this inserts the item
1661 containing "." and ".." entries */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001662static int reiserfs_new_directory(struct reiserfs_transaction_handle *th,
1663 struct inode *inode,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -08001664 struct item_head *ih, struct treepath *path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001665 struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001667 struct super_block *sb = th->t_super;
1668 char empty_dir[EMPTY_DIR_SIZE];
1669 char *body = empty_dir;
1670 struct cpu_key key;
1671 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001673 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001675 _make_cpu_key(&key, KEY_FORMAT_3_5, le32_to_cpu(ih->ih_key.k_dir_id),
1676 le32_to_cpu(ih->ih_key.k_objectid), DOT_OFFSET,
1677 TYPE_DIRENTRY, 3 /*key length */ );
1678
1679 /* compose item head for new item. Directories consist of items of
1680 old type (ITEM_VERSION_1). Do not set key (second arg is 0), it
1681 is done by reiserfs_new_inode */
1682 if (old_format_only(sb)) {
1683 make_le_item_head(ih, NULL, KEY_FORMAT_3_5, DOT_OFFSET,
1684 TYPE_DIRENTRY, EMPTY_DIR_SIZE_V1, 2);
1685
1686 make_empty_dir_item_v1(body, ih->ih_key.k_dir_id,
1687 ih->ih_key.k_objectid,
1688 INODE_PKEY(dir)->k_dir_id,
1689 INODE_PKEY(dir)->k_objectid);
1690 } else {
1691 make_le_item_head(ih, NULL, KEY_FORMAT_3_5, DOT_OFFSET,
1692 TYPE_DIRENTRY, EMPTY_DIR_SIZE, 2);
1693
1694 make_empty_dir_item(body, ih->ih_key.k_dir_id,
1695 ih->ih_key.k_objectid,
1696 INODE_PKEY(dir)->k_dir_id,
1697 INODE_PKEY(dir)->k_objectid);
1698 }
1699
1700 /* look for place in the tree for new item */
1701 retval = search_item(sb, &key, path);
1702 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -04001703 reiserfs_error(sb, "vs-13080",
1704 "i/o failure occurred creating new directory");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001705 return -EIO;
1706 }
1707 if (retval == ITEM_FOUND) {
1708 pathrelse(path);
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001709 reiserfs_warning(sb, "vs-13070",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001710 "object with this key exists (%k)",
1711 &(ih->ih_key));
1712 return -EEXIST;
1713 }
1714
1715 /* insert item, that is empty directory item */
1716 return reiserfs_insert_item(th, path, &key, ih, inode, body);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719/* stat data of object has been inserted, this inserts the item
1720 containing the body of symlink */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001721static int reiserfs_new_symlink(struct reiserfs_transaction_handle *th, struct inode *inode, /* Inode of symlink */
1722 struct item_head *ih,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -08001723 struct treepath *path, const char *symname,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001724 int item_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001726 struct super_block *sb = th->t_super;
1727 struct cpu_key key;
1728 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001730 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001732 _make_cpu_key(&key, KEY_FORMAT_3_5,
1733 le32_to_cpu(ih->ih_key.k_dir_id),
1734 le32_to_cpu(ih->ih_key.k_objectid),
1735 1, TYPE_DIRECT, 3 /*key length */ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001737 make_le_item_head(ih, NULL, KEY_FORMAT_3_5, 1, TYPE_DIRECT, item_len,
1738 0 /*free_space */ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001740 /* look for place in the tree for new item */
1741 retval = search_item(sb, &key, path);
1742 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -04001743 reiserfs_error(sb, "vs-13080",
1744 "i/o failure occurred creating new symlink");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001745 return -EIO;
1746 }
1747 if (retval == ITEM_FOUND) {
1748 pathrelse(path);
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001749 reiserfs_warning(sb, "vs-13080",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001750 "object with this key exists (%k)",
1751 &(ih->ih_key));
1752 return -EEXIST;
1753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001755 /* insert item, that is body of symlink */
1756 return reiserfs_insert_item(th, path, &key, ih, inode, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759/* inserts the stat data into the tree, and then calls
1760 reiserfs_new_directory (to insert ".", ".." item if new object is
1761 directory) or reiserfs_new_symlink (to insert symlink body if new
Jeff Mahoney0222e652009-03-30 14:02:44 -04001762 object is symlink) or nothing (if new object is regular file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 NOTE! uid and gid must already be set in the inode. If we return
1765 non-zero due to an error, we have to drop the quota previously allocated
1766 for the fresh inode. This can only be done outside a transaction, so
1767 if we return non-zero, we also end the transaction. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001768int reiserfs_new_inode(struct reiserfs_transaction_handle *th,
Al Viro8e071892011-07-26 02:50:53 -04001769 struct inode *dir, umode_t mode, const char *symname,
Jeff Mahoney0222e652009-03-30 14:02:44 -04001770 /* 0 for regular, EMTRY_DIR_SIZE for dirs,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001771 strlen (symname) for symlinks) */
1772 loff_t i_size, struct dentry *dentry,
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001773 struct inode *inode,
1774 struct reiserfs_security_handle *security)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001776 struct super_block *sb;
Al Viroc1eaa262008-12-30 02:03:58 -05001777 struct reiserfs_iget_args args;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001778 INITIALIZE_PATH(path_to_key);
1779 struct cpu_key key;
1780 struct item_head ih;
1781 struct stat_data sd;
1782 int retval;
1783 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001785 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Jan Kara7af11682012-11-13 17:05:14 +01001787 reiserfs_write_unlock(inode->i_sb);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001788 err = dquot_alloc_inode(inode);
Jan Kara7af11682012-11-13 17:05:14 +01001789 reiserfs_write_lock(inode->i_sb);
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001790 if (err)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001791 goto out_end_trans;
Eric Sesterhenn585b7742006-10-04 02:15:30 -07001792 if (!dir->i_nlink) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001793 err = -EPERM;
1794 goto out_bad_inode;
1795 }
1796
1797 sb = dir->i_sb;
1798
1799 /* item head of new item */
1800 ih.ih_key.k_dir_id = reiserfs_choose_packing(dir);
1801 ih.ih_key.k_objectid = cpu_to_le32(reiserfs_get_unused_objectid(th));
1802 if (!ih.ih_key.k_objectid) {
1803 err = -ENOMEM;
1804 goto out_bad_inode;
1805 }
Al Viroc1eaa262008-12-30 02:03:58 -05001806 args.objectid = inode->i_ino = le32_to_cpu(ih.ih_key.k_objectid);
Al Viro2f1169e2009-01-02 08:16:51 -05001807 if (old_format_only(sb))
1808 make_le_item_head(&ih, NULL, KEY_FORMAT_3_5, SD_OFFSET,
1809 TYPE_STAT_DATA, SD_V1_SIZE, MAX_US_INT);
1810 else
1811 make_le_item_head(&ih, NULL, KEY_FORMAT_3_6, SD_OFFSET,
1812 TYPE_STAT_DATA, SD_SIZE, MAX_US_INT);
Al Viroc1eaa262008-12-30 02:03:58 -05001813 memcpy(INODE_PKEY(inode), &(ih.ih_key), KEY_SIZE);
1814 args.dirid = le32_to_cpu(ih.ih_key.k_dir_id);
Jeff Mahoneya1457c02013-05-31 15:51:17 -04001815
1816 reiserfs_write_unlock(inode->i_sb);
1817 err = insert_inode_locked4(inode, args.objectid,
1818 reiserfs_find_actor, &args);
1819 reiserfs_write_lock(inode->i_sb);
1820 if (err) {
Al Viroc1eaa262008-12-30 02:03:58 -05001821 err = -EINVAL;
1822 goto out_bad_inode;
1823 }
Jeff Mahoneya1457c02013-05-31 15:51:17 -04001824
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001825 if (old_format_only(sb))
Jeff Mahoney0222e652009-03-30 14:02:44 -04001826 /* not a perfect generation count, as object ids can be reused, but
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001827 ** this is as good as reiserfs can do right now.
1828 ** note that the private part of inode isn't filled in yet, we have
1829 ** to use the directory.
1830 */
1831 inode->i_generation = le32_to_cpu(INODE_PKEY(dir)->k_objectid);
1832 else
1833#if defined( USE_INODE_GENERATION_COUNTER )
1834 inode->i_generation =
1835 le32_to_cpu(REISERFS_SB(sb)->s_rs->s_inode_generation);
1836#else
1837 inode->i_generation = ++event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001840 /* fill stat data */
Miklos Szeredibfe86842011-10-28 14:13:29 +02001841 set_nlink(inode, (S_ISDIR(mode) ? 2 : 1));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001842
1843 /* uid and gid must already be set by the caller for quota init */
1844
1845 /* symlink cannot be immutable or append only, right? */
1846 if (S_ISLNK(inode->i_mode))
1847 inode->i_flags &= ~(S_IMMUTABLE | S_APPEND);
1848
1849 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
1850 inode->i_size = i_size;
1851 inode->i_blocks = 0;
1852 inode->i_bytes = 0;
1853 REISERFS_I(inode)->i_first_direct_byte = S_ISLNK(mode) ? 1 :
1854 U32_MAX /*NO_BYTES_IN_DIRECT_ITEM */ ;
1855
1856 INIT_LIST_HEAD(&(REISERFS_I(inode)->i_prealloc_list));
1857 REISERFS_I(inode)->i_flags = 0;
1858 REISERFS_I(inode)->i_prealloc_block = 0;
1859 REISERFS_I(inode)->i_prealloc_count = 0;
1860 REISERFS_I(inode)->i_trans_id = 0;
1861 REISERFS_I(inode)->i_jl = NULL;
1862 REISERFS_I(inode)->i_attrs =
1863 REISERFS_I(dir)->i_attrs & REISERFS_INHERIT_MASK;
1864 sd_attrs_to_i_attrs(REISERFS_I(inode)->i_attrs, inode);
Alexey Dobriyan068fbb32006-09-29 01:59:58 -07001865 reiserfs_init_xattr_rwsem(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001866
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001867 /* key to search for correct place for new stat data */
1868 _make_cpu_key(&key, KEY_FORMAT_3_6, le32_to_cpu(ih.ih_key.k_dir_id),
1869 le32_to_cpu(ih.ih_key.k_objectid), SD_OFFSET,
1870 TYPE_STAT_DATA, 3 /*key length */ );
1871
1872 /* find proper place for inserting of stat data */
1873 retval = search_item(sb, &key, &path_to_key);
1874 if (retval == IO_ERROR) {
1875 err = -EIO;
1876 goto out_bad_inode;
1877 }
1878 if (retval == ITEM_FOUND) {
1879 pathrelse(&path_to_key);
1880 err = -EEXIST;
1881 goto out_bad_inode;
1882 }
1883 if (old_format_only(sb)) {
Eric W. Biedermandf814652012-02-07 16:30:06 -08001884 if (i_uid_read(inode) & ~0xffff || i_gid_read(inode) & ~0xffff) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001885 pathrelse(&path_to_key);
1886 /* i_uid or i_gid is too big to be stored in stat data v3.5 */
1887 err = -EINVAL;
1888 goto out_bad_inode;
1889 }
1890 inode2sd_v1(&sd, inode, inode->i_size);
1891 } else {
1892 inode2sd(&sd, inode, inode->i_size);
1893 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001894 // store in in-core inode the key of stat data and version all
1895 // object items will have (directory items will have old offset
1896 // format, other new objects will consist of new items)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001897 if (old_format_only(sb) || S_ISDIR(mode) || S_ISLNK(mode))
1898 set_inode_item_key_version(inode, KEY_FORMAT_3_5);
1899 else
1900 set_inode_item_key_version(inode, KEY_FORMAT_3_6);
1901 if (old_format_only(sb))
1902 set_inode_sd_version(inode, STAT_DATA_V1);
1903 else
1904 set_inode_sd_version(inode, STAT_DATA_V2);
1905
1906 /* insert the stat data into the tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001908 if (REISERFS_I(dir)->new_packing_locality)
1909 th->displace_new_blocks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001911 retval =
1912 reiserfs_insert_item(th, &path_to_key, &key, &ih, inode,
1913 (char *)(&sd));
1914 if (retval) {
1915 err = retval;
1916 reiserfs_check_path(&path_to_key);
1917 goto out_bad_inode;
1918 }
1919#ifdef DISPLACE_NEW_PACKING_LOCALITIES
1920 if (!th->displace_new_blocks)
1921 REISERFS_I(dir)->new_packing_locality = 0;
1922#endif
1923 if (S_ISDIR(mode)) {
1924 /* insert item with "." and ".." */
1925 retval =
1926 reiserfs_new_directory(th, inode, &ih, &path_to_key, dir);
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001929 if (S_ISLNK(mode)) {
1930 /* insert body of symlink */
1931 if (!old_format_only(sb))
1932 i_size = ROUND_UP(i_size);
1933 retval =
1934 reiserfs_new_symlink(th, inode, &ih, &path_to_key, symname,
1935 i_size);
1936 }
1937 if (retval) {
1938 err = retval;
1939 reiserfs_check_path(&path_to_key);
1940 journal_end(th, th->t_super, th->t_blocks_allocated);
1941 goto out_inserted_sd;
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001944 if (reiserfs_posixacl(inode->i_sb)) {
Jeff Mahoney4c051412013-08-08 17:27:19 -04001945 reiserfs_write_unlock(inode->i_sb);
Jeff Mahoney0ab26212009-03-30 14:02:39 -04001946 retval = reiserfs_inherit_default_acl(th, dir, dentry, inode);
Jeff Mahoney4c051412013-08-08 17:27:19 -04001947 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001948 if (retval) {
1949 err = retval;
1950 reiserfs_check_path(&path_to_key);
1951 journal_end(th, th->t_super, th->t_blocks_allocated);
1952 goto out_inserted_sd;
1953 }
1954 } else if (inode->i_sb->s_flags & MS_POSIXACL) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001955 reiserfs_warning(inode->i_sb, "jdm-13090",
1956 "ACLs aren't enabled in the fs, "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001957 "but vfs thinks they are!");
Jeff Mahoney6dfede692009-03-30 14:02:32 -04001958 } else if (IS_PRIVATE(dir))
1959 inode->i_flags |= S_PRIVATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001961 if (security->name) {
Jeff Mahoney4c051412013-08-08 17:27:19 -04001962 reiserfs_write_unlock(inode->i_sb);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001963 retval = reiserfs_security_write(th, inode, security);
Jeff Mahoney4c051412013-08-08 17:27:19 -04001964 reiserfs_write_lock(inode->i_sb);
Jeff Mahoney57fe60d2009-03-30 14:02:41 -04001965 if (retval) {
1966 err = retval;
1967 reiserfs_check_path(&path_to_key);
1968 retval = journal_end(th, th->t_super,
1969 th->t_blocks_allocated);
1970 if (retval)
1971 err = retval;
1972 goto out_inserted_sd;
1973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001976 reiserfs_update_sd(th, inode);
1977 reiserfs_check_path(&path_to_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981/* it looks like you can easily compress these two goto targets into
1982 * one. Keeping it like this doesn't actually hurt anything, and they
1983 * are place holders for what the quota code actually needs.
1984 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001985 out_bad_inode:
1986 /* Invalidate the object, nothing was inserted yet */
1987 INODE_PKEY(inode)->k_objectid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001989 /* Quota change must be inside a transaction for journaling */
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001990 dquot_free_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001992 out_end_trans:
1993 journal_end(th, th->t_super, th->t_blocks_allocated);
Jan Kara7af11682012-11-13 17:05:14 +01001994 reiserfs_write_unlock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001995 /* Drop can be outside and it needs more credits so it's better to have it outside */
Christoph Hellwig9f754752010-03-03 09:05:05 -05001996 dquot_drop(inode);
Jan Kara7af11682012-11-13 17:05:14 +01001997 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001998 inode->i_flags |= S_NOQUOTA;
1999 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002001 out_inserted_sd:
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02002002 clear_nlink(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002003 th->t_trans_id = 0; /* so the caller can't use this handle later */
Al Viroc1eaa262008-12-30 02:03:58 -05002004 unlock_new_inode(inode); /* OK to do even if we hadn't locked it */
Jeff Mahoneyd9845612009-03-30 14:02:35 -04002005 iput(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002006 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
2009/*
2010** finds the tail page in the page cache,
2011** reads the last block in.
2012**
2013** On success, page_result is set to a locked, pinned page, and bh_result
2014** is set to an up to date buffer for the last block in the file. returns 0.
2015**
2016** tail conversion is not done, so bh_result might not be valid for writing
2017** check buffer_mapped(bh_result) and bh_result->b_blocknr != 0 before
2018** trying to write the block.
2019**
2020** on failure, nonzero is returned, page_result and bh_result are untouched.
2021*/
Jeff Mahoney995c7622009-03-30 14:02:47 -04002022static int grab_tail_page(struct inode *inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002023 struct page **page_result,
2024 struct buffer_head **bh_result)
2025{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002027 /* we want the page with the last byte in the file,
2028 ** not the page that will hold the next byte for appending
2029 */
Jeff Mahoney995c7622009-03-30 14:02:47 -04002030 unsigned long index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002031 unsigned long pos = 0;
2032 unsigned long start = 0;
Jeff Mahoney995c7622009-03-30 14:02:47 -04002033 unsigned long blocksize = inode->i_sb->s_blocksize;
2034 unsigned long offset = (inode->i_size) & (PAGE_CACHE_SIZE - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002035 struct buffer_head *bh;
2036 struct buffer_head *head;
2037 struct page *page;
2038 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002040 /* we know that we are only called with inode->i_size > 0.
2041 ** we also know that a file tail can never be as big as a block
2042 ** If i_size % blocksize == 0, our file is currently block aligned
2043 ** and it won't need converting or zeroing after a truncate.
2044 */
2045 if ((offset & (blocksize - 1)) == 0) {
2046 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Jeff Mahoney995c7622009-03-30 14:02:47 -04002048 page = grab_cache_page(inode->i_mapping, index);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002049 error = -ENOMEM;
2050 if (!page) {
2051 goto out;
2052 }
2053 /* start within the page of the last block in the file */
2054 start = (offset / blocksize) * blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Christoph Hellwigebdec242010-10-06 10:47:23 +02002056 error = __block_write_begin(page, start, offset - start,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002057 reiserfs_get_block_create_0);
2058 if (error)
2059 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002061 head = page_buffers(page);
2062 bh = head;
2063 do {
2064 if (pos >= start) {
2065 break;
2066 }
2067 bh = bh->b_this_page;
2068 pos += blocksize;
2069 } while (bh != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002071 if (!buffer_uptodate(bh)) {
2072 /* note, this should never happen, prepare_write should
2073 ** be taking care of this for us. If the buffer isn't up to date,
2074 ** I've screwed up the code to find the buffer, or the code to
2075 ** call prepare_write
2076 */
Jeff Mahoney995c7622009-03-30 14:02:47 -04002077 reiserfs_error(inode->i_sb, "clm-6000",
Jeff Mahoney0030b642009-03-30 14:02:28 -04002078 "error reading block %lu", bh->b_blocknr);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002079 error = -EIO;
2080 goto unlock;
2081 }
2082 *bh_result = bh;
2083 *page_result = page;
2084
2085 out:
2086 return error;
2087
2088 unlock:
2089 unlock_page(page);
2090 page_cache_release(page);
2091 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
2094/*
2095** vfs version of truncate file. Must NOT be called with
2096** a transaction already started.
2097**
2098** some code taken from block_truncate_page
2099*/
Jeff Mahoney995c7622009-03-30 14:02:47 -04002100int reiserfs_truncate_file(struct inode *inode, int update_timestamps)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002101{
2102 struct reiserfs_transaction_handle th;
2103 /* we want the offset for the first byte after the end of the file */
Jeff Mahoney995c7622009-03-30 14:02:47 -04002104 unsigned long offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
2105 unsigned blocksize = inode->i_sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002106 unsigned length;
2107 struct page *page = NULL;
2108 int error;
2109 struct buffer_head *bh = NULL;
Jeff Mahoney24996042005-12-14 14:38:05 -05002110 int err2;
Frederic Weisbecker22c963a2009-04-14 05:34:24 +02002111 int lock_depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Frederic Weisbecker22c963a2009-04-14 05:34:24 +02002113 lock_depth = reiserfs_write_lock_once(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Jeff Mahoney995c7622009-03-30 14:02:47 -04002115 if (inode->i_size > 0) {
2116 error = grab_tail_page(inode, &page, &bh);
2117 if (error) {
Jeff Mahoney0222e652009-03-30 14:02:44 -04002118 // -ENOENT means we truncated past the end of the file,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002119 // and get_block_create_0 could not find a block to read in,
2120 // which is ok.
2121 if (error != -ENOENT)
Jeff Mahoney995c7622009-03-30 14:02:47 -04002122 reiserfs_error(inode->i_sb, "clm-6001",
Jeff Mahoney0030b642009-03-30 14:02:28 -04002123 "grab_tail_page failed %d",
2124 error);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002125 page = NULL;
2126 bh = NULL;
2127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Jeff Mahoney0222e652009-03-30 14:02:44 -04002130 /* so, if page != NULL, we have a buffer head for the offset at
2131 ** the end of the file. if the bh is mapped, and bh->b_blocknr != 0,
2132 ** then we have an unformatted node. Otherwise, we have a direct item,
2133 ** and no zeroing is required on disk. We zero after the truncate,
2134 ** because the truncate might pack the item anyway
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002135 ** (it will unmap bh if it packs).
2136 */
2137 /* it is enough to reserve space in transaction for 2 balancings:
2138 one for "save" link adding and another for the first
2139 cut_from_item. 1 is for update_sd */
Jeff Mahoney995c7622009-03-30 14:02:47 -04002140 error = journal_begin(&th, inode->i_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002141 JOURNAL_PER_BALANCE_CNT * 2 + 1);
2142 if (error)
2143 goto out;
Jeff Mahoney995c7622009-03-30 14:02:47 -04002144 reiserfs_update_inode_transaction(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002145 if (update_timestamps)
2146 /* we are doing real truncate: if the system crashes before the last
2147 transaction of truncating gets committed - on reboot the file
2148 either appears truncated properly or not truncated at all */
Jeff Mahoney995c7622009-03-30 14:02:47 -04002149 add_save_link(&th, inode, 1);
2150 err2 = reiserfs_do_truncate(&th, inode, page, update_timestamps);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002151 error =
Jeff Mahoney995c7622009-03-30 14:02:47 -04002152 journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 2 + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002153 if (error)
2154 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Jeff Mahoney24996042005-12-14 14:38:05 -05002156 /* check reiserfs_do_truncate after ending the transaction */
2157 if (err2) {
2158 error = err2;
2159 goto out;
2160 }
2161
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002162 if (update_timestamps) {
Jeff Mahoney995c7622009-03-30 14:02:47 -04002163 error = remove_save_link(inode, 1 /* truncate */);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002164 if (error)
2165 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002168 if (page) {
2169 length = offset & (blocksize - 1);
2170 /* if we are not on a block boundary */
2171 if (length) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002172 length = blocksize - length;
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002173 zero_user(page, offset, length);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002174 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
2175 mark_buffer_dirty(bh);
2176 }
2177 }
2178 unlock_page(page);
2179 page_cache_release(page);
2180 }
2181
Frederic Weisbecker22c963a2009-04-14 05:34:24 +02002182 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
2183
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002184 return 0;
2185 out:
2186 if (page) {
2187 unlock_page(page);
2188 page_cache_release(page);
2189 }
Frederic Weisbecker22c963a2009-04-14 05:34:24 +02002190
2191 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
2192
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002193 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002196static int map_block_for_writepage(struct inode *inode,
2197 struct buffer_head *bh_result,
2198 unsigned long block)
2199{
2200 struct reiserfs_transaction_handle th;
2201 int fs_gen;
2202 struct item_head tmp_ih;
2203 struct item_head *ih;
2204 struct buffer_head *bh;
2205 __le32 *item;
2206 struct cpu_key key;
2207 INITIALIZE_PATH(path);
2208 int pos_in_item;
2209 int jbegin_count = JOURNAL_PER_BALANCE_CNT;
Oleg Drokin7729ac52005-11-28 13:43:53 -08002210 loff_t byte_offset = ((loff_t)block << inode->i_sb->s_blocksize_bits)+1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002211 int retval;
2212 int use_get_block = 0;
2213 int bytes_copied = 0;
2214 int copy_size;
2215 int trans_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002217 /* catch places below that try to log something without starting a trans */
2218 th.t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002220 if (!buffer_uptodate(bh_result)) {
2221 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002224 kmap(bh_result->b_page);
2225 start_over:
2226 reiserfs_write_lock(inode->i_sb);
2227 make_cpu_key(&key, inode, byte_offset, TYPE_ANY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002229 research:
2230 retval = search_for_position_by_key(inode->i_sb, &key, &path);
2231 if (retval != POSITION_FOUND) {
2232 use_get_block = 1;
2233 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
2235
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002236 bh = get_last_bh(&path);
2237 ih = get_ih(&path);
2238 item = get_item(&path);
2239 pos_in_item = path.pos_in_item;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002241 /* we've found an unformatted node */
2242 if (indirect_item_found(retval, ih)) {
2243 if (bytes_copied > 0) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002244 reiserfs_warning(inode->i_sb, "clm-6002",
2245 "bytes_copied %d", bytes_copied);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002246 }
2247 if (!get_block_num(item, pos_in_item)) {
2248 /* crap, we are writing to a hole */
2249 use_get_block = 1;
2250 goto out;
2251 }
2252 set_block_dev_mapped(bh_result,
2253 get_block_num(item, pos_in_item), inode);
2254 } else if (is_direct_le_ih(ih)) {
2255 char *p;
2256 p = page_address(bh_result->b_page);
2257 p += (byte_offset - 1) & (PAGE_CACHE_SIZE - 1);
2258 copy_size = ih_item_len(ih) - pos_in_item;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002260 fs_gen = get_generation(inode->i_sb);
2261 copy_item_head(&tmp_ih, ih);
2262
2263 if (!trans_running) {
2264 /* vs-3050 is gone, no need to drop the path */
2265 retval = journal_begin(&th, inode->i_sb, jbegin_count);
2266 if (retval)
2267 goto out;
2268 reiserfs_update_inode_transaction(inode);
2269 trans_running = 1;
2270 if (fs_changed(fs_gen, inode->i_sb)
2271 && item_moved(&tmp_ih, &path)) {
2272 reiserfs_restore_prepared_buffer(inode->i_sb,
2273 bh);
2274 goto research;
2275 }
2276 }
2277
2278 reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
2279
2280 if (fs_changed(fs_gen, inode->i_sb)
2281 && item_moved(&tmp_ih, &path)) {
2282 reiserfs_restore_prepared_buffer(inode->i_sb, bh);
2283 goto research;
2284 }
2285
2286 memcpy(B_I_PITEM(bh, ih) + pos_in_item, p + bytes_copied,
2287 copy_size);
2288
2289 journal_mark_dirty(&th, inode->i_sb, bh);
2290 bytes_copied += copy_size;
2291 set_block_dev_mapped(bh_result, 0, inode);
2292
2293 /* are there still bytes left? */
2294 if (bytes_copied < bh_result->b_size &&
2295 (byte_offset + bytes_copied) < inode->i_size) {
2296 set_cpu_key_k_offset(&key,
2297 cpu_key_k_offset(&key) +
2298 copy_size);
2299 goto research;
2300 }
2301 } else {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002302 reiserfs_warning(inode->i_sb, "clm-6003",
2303 "bad item inode %lu", inode->i_ino);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002304 retval = -EIO;
2305 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002307 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002309 out:
2310 pathrelse(&path);
2311 if (trans_running) {
2312 int err = journal_end(&th, inode->i_sb, jbegin_count);
2313 if (err)
2314 retval = err;
2315 trans_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002317 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002319 /* this is where we fill in holes in the file. */
2320 if (use_get_block) {
2321 retval = reiserfs_get_block(inode, block, bh_result,
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002322 GET_BLOCK_CREATE | GET_BLOCK_NO_IMUX
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002323 | GET_BLOCK_NO_DANGLE);
2324 if (!retval) {
2325 if (!buffer_mapped(bh_result)
2326 || bh_result->b_blocknr == 0) {
2327 /* get_block failed to find a mapped unformatted node. */
2328 use_get_block = 0;
2329 goto start_over;
2330 }
2331 }
2332 }
2333 kunmap(bh_result->b_page);
2334
2335 if (!retval && buffer_mapped(bh_result) && bh_result->b_blocknr == 0) {
2336 /* we've copied data from the page into the direct item, so the
2337 * buffer in the page is now clean, mark it to reflect that.
2338 */
2339 lock_buffer(bh_result);
2340 clear_buffer_dirty(bh_result);
2341 unlock_buffer(bh_result);
2342 }
2343 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
Jeff Mahoney0222e652009-03-30 14:02:44 -04002346/*
2347 * mason@suse.com: updated in 2.5.54 to follow the same general io
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 * start/recovery path as __block_write_full_page, along with special
2349 * code to handle reiserfs tails.
2350 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002351static int reiserfs_write_full_page(struct page *page,
2352 struct writeback_control *wbc)
2353{
2354 struct inode *inode = page->mapping->host;
2355 unsigned long end_index = inode->i_size >> PAGE_CACHE_SHIFT;
2356 int error = 0;
2357 unsigned long block;
Chris Masonb4c76fa2006-08-05 12:15:10 -07002358 sector_t last_block;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002359 struct buffer_head *head, *bh;
2360 int partial = 0;
2361 int nr = 0;
2362 int checked = PageChecked(page);
2363 struct reiserfs_transaction_handle th;
2364 struct super_block *s = inode->i_sb;
2365 int bh_per_page = PAGE_CACHE_SIZE / s->s_blocksize;
2366 th.t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Chris Masone0e851c2006-02-01 03:06:49 -08002368 /* no logging allowed when nonblocking or from PF_MEMALLOC */
2369 if (checked && (current->flags & PF_MEMALLOC)) {
2370 redirty_page_for_writepage(wbc, page);
2371 unlock_page(page);
2372 return 0;
2373 }
2374
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002375 /* The page dirty bit is cleared before writepage is called, which
2376 * means we have to tell create_empty_buffers to make dirty buffers
2377 * The page really should be up to date at this point, so tossing
2378 * in the BH_Uptodate is just a sanity check.
2379 */
2380 if (!page_has_buffers(page)) {
2381 create_empty_buffers(page, s->s_blocksize,
2382 (1 << BH_Dirty) | (1 << BH_Uptodate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002384 head = page_buffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002386 /* last page in the file, zero out any contents past the
2387 ** last byte in the file
2388 */
2389 if (page->index >= end_index) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002390 unsigned last_offset;
2391
2392 last_offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
2393 /* no file contents in this page */
2394 if (page->index >= end_index + 1 || !last_offset) {
2395 unlock_page(page);
2396 return 0;
2397 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002398 zero_user_segment(page, last_offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002400 bh = head;
2401 block = page->index << (PAGE_CACHE_SHIFT - s->s_blocksize_bits);
Chris Masonb4c76fa2006-08-05 12:15:10 -07002402 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002403 /* first map all the buffers, logging any direct items we find */
2404 do {
Chris Masonb4c76fa2006-08-05 12:15:10 -07002405 if (block > last_block) {
2406 /*
2407 * This can happen when the block size is less than
2408 * the page size. The corresponding bytes in the page
2409 * were zero filled above
2410 */
2411 clear_buffer_dirty(bh);
2412 set_buffer_uptodate(bh);
2413 } else if ((checked || buffer_dirty(bh)) &&
2414 (!buffer_mapped(bh) || (buffer_mapped(bh)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002415 && bh->b_blocknr ==
2416 0))) {
2417 /* not mapped yet, or it points to a direct item, search
2418 * the btree for the mapping info, and log any direct
2419 * items found
2420 */
2421 if ((error = map_block_for_writepage(inode, bh, block))) {
2422 goto fail;
2423 }
2424 }
2425 bh = bh->b_this_page;
2426 block++;
2427 } while (bh != head);
2428
2429 /*
2430 * we start the transaction after map_block_for_writepage,
2431 * because it can create holes in the file (an unbounded operation).
2432 * starting it here, we can make a reliable estimate for how many
2433 * blocks we're going to log
2434 */
2435 if (checked) {
2436 ClearPageChecked(page);
2437 reiserfs_write_lock(s);
2438 error = journal_begin(&th, s, bh_per_page + 1);
2439 if (error) {
2440 reiserfs_write_unlock(s);
2441 goto fail;
2442 }
2443 reiserfs_update_inode_transaction(inode);
2444 }
2445 /* now go through and lock any dirty buffers on the page */
2446 do {
2447 get_bh(bh);
2448 if (!buffer_mapped(bh))
2449 continue;
2450 if (buffer_mapped(bh) && bh->b_blocknr == 0)
2451 continue;
2452
2453 if (checked) {
2454 reiserfs_prepare_for_journal(s, bh, 1);
2455 journal_mark_dirty(&th, s, bh);
2456 continue;
2457 }
2458 /* from this point on, we know the buffer is mapped to a
2459 * real block and not a direct item
2460 */
Wu Fengguang1b430be2010-10-26 14:21:26 -07002461 if (wbc->sync_mode != WB_SYNC_NONE) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002462 lock_buffer(bh);
2463 } else {
Nick Pigginca5de402008-08-02 12:02:13 +02002464 if (!trylock_buffer(bh)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002465 redirty_page_for_writepage(wbc, page);
2466 continue;
2467 }
2468 }
2469 if (test_clear_buffer_dirty(bh)) {
2470 mark_buffer_async_write(bh);
2471 } else {
2472 unlock_buffer(bh);
2473 }
2474 } while ((bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 if (checked) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002477 error = journal_end(&th, s, bh_per_page + 1);
2478 reiserfs_write_unlock(s);
2479 if (error)
2480 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002482 BUG_ON(PageWriteback(page));
2483 set_page_writeback(page);
2484 unlock_page(page);
2485
2486 /*
Jeff Mahoney0222e652009-03-30 14:02:44 -04002487 * since any buffer might be the only dirty buffer on the page,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002488 * the first submit_bh can bring the page out of writeback.
2489 * be careful with the buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 do {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002492 struct buffer_head *next = bh->b_this_page;
2493 if (buffer_async_write(bh)) {
2494 submit_bh(WRITE, bh);
2495 nr++;
2496 }
2497 put_bh(bh);
2498 bh = next;
2499 } while (bh != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002501 error = 0;
2502 done:
2503 if (nr == 0) {
2504 /*
2505 * if this page only had a direct item, it is very possible for
Jeff Mahoney0222e652009-03-30 14:02:44 -04002506 * no io to be required without there being an error. Or,
2507 * someone else could have locked them and sent them down the
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002508 * pipe without locking the page
2509 */
2510 bh = head;
2511 do {
2512 if (!buffer_uptodate(bh)) {
2513 partial = 1;
2514 break;
2515 }
2516 bh = bh->b_this_page;
2517 } while (bh != head);
2518 if (!partial)
2519 SetPageUptodate(page);
2520 end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002522 return error;
2523
2524 fail:
2525 /* catches various errors, we need to make sure any valid dirty blocks
Jeff Mahoney0222e652009-03-30 14:02:44 -04002526 * get to the media. The page is currently locked and not marked for
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002527 * writeback
2528 */
2529 ClearPageUptodate(page);
2530 bh = head;
2531 do {
2532 get_bh(bh);
2533 if (buffer_mapped(bh) && buffer_dirty(bh) && bh->b_blocknr) {
2534 lock_buffer(bh);
2535 mark_buffer_async_write(bh);
2536 } else {
2537 /*
2538 * clear any dirty bits that might have come from getting
2539 * attached to a dirty page
2540 */
2541 clear_buffer_dirty(bh);
2542 }
2543 bh = bh->b_this_page;
2544 } while (bh != head);
2545 SetPageError(page);
2546 BUG_ON(PageWriteback(page));
2547 set_page_writeback(page);
2548 unlock_page(page);
2549 do {
2550 struct buffer_head *next = bh->b_this_page;
2551 if (buffer_async_write(bh)) {
2552 clear_buffer_dirty(bh);
2553 submit_bh(WRITE, bh);
2554 nr++;
2555 }
2556 put_bh(bh);
2557 bh = next;
2558 } while (bh != head);
2559 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002562static int reiserfs_readpage(struct file *f, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002564 return block_read_full_page(page, reiserfs_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002567static int reiserfs_writepage(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002569 struct inode *inode = page->mapping->host;
2570 reiserfs_wait_on_write_block(inode->i_sb);
2571 return reiserfs_write_full_page(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
Jan Karaec8e2f72009-12-17 15:27:06 -08002574static void reiserfs_truncate_failed_write(struct inode *inode)
2575{
2576 truncate_inode_pages(inode->i_mapping, inode->i_size);
2577 reiserfs_truncate_file(inode, 0);
2578}
2579
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002580static int reiserfs_write_begin(struct file *file,
2581 struct address_space *mapping,
2582 loff_t pos, unsigned len, unsigned flags,
2583 struct page **pagep, void **fsdata)
2584{
2585 struct inode *inode;
2586 struct page *page;
2587 pgoff_t index;
2588 int ret;
2589 int old_ref = 0;
2590
Vladimir Savelievf7557e82007-10-16 01:25:14 -07002591 inode = mapping->host;
2592 *fsdata = 0;
2593 if (flags & AOP_FLAG_CONT_EXPAND &&
2594 (pos & (inode->i_sb->s_blocksize - 1)) == 0) {
2595 pos ++;
2596 *fsdata = (void *)(unsigned long)flags;
2597 }
2598
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002599 index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin54566b22009-01-04 12:00:53 -08002600 page = grab_cache_page_write_begin(mapping, index, flags);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002601 if (!page)
2602 return -ENOMEM;
2603 *pagep = page;
2604
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002605 reiserfs_wait_on_write_block(inode->i_sb);
2606 fix_tail_page_for_writing(page);
2607 if (reiserfs_transaction_running(inode->i_sb)) {
2608 struct reiserfs_transaction_handle *th;
2609 th = (struct reiserfs_transaction_handle *)current->
2610 journal_info;
2611 BUG_ON(!th->t_refcount);
2612 BUG_ON(!th->t_trans_id);
2613 old_ref = th->t_refcount;
2614 th->t_refcount++;
2615 }
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002616 ret = __block_write_begin(page, pos, len, reiserfs_get_block);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002617 if (ret && reiserfs_transaction_running(inode->i_sb)) {
2618 struct reiserfs_transaction_handle *th = current->journal_info;
2619 /* this gets a little ugly. If reiserfs_get_block returned an
2620 * error and left a transacstion running, we've got to close it,
2621 * and we've got to free handle if it was a persistent transaction.
2622 *
2623 * But, if we had nested into an existing transaction, we need
2624 * to just drop the ref count on the handle.
2625 *
2626 * If old_ref == 0, the transaction is from reiserfs_get_block,
2627 * and it was a persistent trans. Otherwise, it was nested above.
2628 */
2629 if (th->t_refcount > old_ref) {
2630 if (old_ref)
2631 th->t_refcount--;
2632 else {
2633 int err;
2634 reiserfs_write_lock(inode->i_sb);
2635 err = reiserfs_end_persistent_transaction(th);
2636 reiserfs_write_unlock(inode->i_sb);
2637 if (err)
2638 ret = err;
2639 }
2640 }
2641 }
2642 if (ret) {
2643 unlock_page(page);
2644 page_cache_release(page);
Jan Karaec8e2f72009-12-17 15:27:06 -08002645 /* Truncate allocated blocks */
2646 reiserfs_truncate_failed_write(inode);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002647 }
2648 return ret;
2649}
2650
Christoph Hellwigebdec242010-10-06 10:47:23 +02002651int __reiserfs_write_begin(struct page *page, unsigned from, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002653 struct inode *inode = page->mapping->host;
2654 int ret;
2655 int old_ref = 0;
2656
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002657 reiserfs_write_unlock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002658 reiserfs_wait_on_write_block(inode->i_sb);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002659 reiserfs_write_lock(inode->i_sb);
2660
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002661 fix_tail_page_for_writing(page);
2662 if (reiserfs_transaction_running(inode->i_sb)) {
2663 struct reiserfs_transaction_handle *th;
2664 th = (struct reiserfs_transaction_handle *)current->
2665 journal_info;
2666 BUG_ON(!th->t_refcount);
2667 BUG_ON(!th->t_trans_id);
2668 old_ref = th->t_refcount;
2669 th->t_refcount++;
2670 }
2671
Christoph Hellwigebdec242010-10-06 10:47:23 +02002672 ret = __block_write_begin(page, from, len, reiserfs_get_block);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002673 if (ret && reiserfs_transaction_running(inode->i_sb)) {
2674 struct reiserfs_transaction_handle *th = current->journal_info;
2675 /* this gets a little ugly. If reiserfs_get_block returned an
2676 * error and left a transacstion running, we've got to close it,
2677 * and we've got to free handle if it was a persistent transaction.
2678 *
2679 * But, if we had nested into an existing transaction, we need
2680 * to just drop the ref count on the handle.
2681 *
2682 * If old_ref == 0, the transaction is from reiserfs_get_block,
2683 * and it was a persistent trans. Otherwise, it was nested above.
2684 */
2685 if (th->t_refcount > old_ref) {
2686 if (old_ref)
2687 th->t_refcount--;
2688 else {
2689 int err;
2690 reiserfs_write_lock(inode->i_sb);
2691 err = reiserfs_end_persistent_transaction(th);
2692 reiserfs_write_unlock(inode->i_sb);
2693 if (err)
2694 ret = err;
2695 }
2696 }
2697 }
2698 return ret;
2699
2700}
2701
2702static sector_t reiserfs_aop_bmap(struct address_space *as, sector_t block)
2703{
2704 return generic_block_bmap(as, block, reiserfs_bmap);
2705}
2706
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002707static int reiserfs_write_end(struct file *file, struct address_space *mapping,
2708 loff_t pos, unsigned len, unsigned copied,
2709 struct page *page, void *fsdata)
2710{
2711 struct inode *inode = page->mapping->host;
2712 int ret = 0;
2713 int update_sd = 0;
2714 struct reiserfs_transaction_handle *th;
2715 unsigned start;
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002716 int lock_depth = 0;
2717 bool locked = false;
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002718
Vladimir Savelievf7557e82007-10-16 01:25:14 -07002719 if ((unsigned long)fsdata & AOP_FLAG_CONT_EXPAND)
2720 pos ++;
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002721
2722 reiserfs_wait_on_write_block(inode->i_sb);
2723 if (reiserfs_transaction_running(inode->i_sb))
2724 th = current->journal_info;
2725 else
2726 th = NULL;
2727
2728 start = pos & (PAGE_CACHE_SIZE - 1);
2729 if (unlikely(copied < len)) {
2730 if (!PageUptodate(page))
2731 copied = 0;
2732
2733 page_zero_new_buffers(page, start + copied, start + len);
2734 }
2735 flush_dcache_page(page);
2736
2737 reiserfs_commit_page(inode, page, start, start + copied);
2738
2739 /* generic_commit_write does this for us, but does not update the
2740 ** transaction tracking stuff when the size changes. So, we have
2741 ** to do the i_size updates here.
2742 */
Jan Karaec8e2f72009-12-17 15:27:06 -08002743 if (pos + copied > inode->i_size) {
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002744 struct reiserfs_transaction_handle myth;
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002745 lock_depth = reiserfs_write_lock_once(inode->i_sb);
2746 locked = true;
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002747 /* If the file have grown beyond the border where it
2748 can have a tail, unmark it as needing a tail
2749 packing */
2750 if ((have_large_tails(inode->i_sb)
2751 && inode->i_size > i_block_size(inode) * 4)
2752 || (have_small_tails(inode->i_sb)
2753 && inode->i_size > i_block_size(inode)))
2754 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
2755
2756 ret = journal_begin(&myth, inode->i_sb, 1);
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002757 if (ret)
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002758 goto journal_error;
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002759
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002760 reiserfs_update_inode_transaction(inode);
Jan Karaec8e2f72009-12-17 15:27:06 -08002761 inode->i_size = pos + copied;
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002762 /*
2763 * this will just nest into our transaction. It's important
2764 * to use mark_inode_dirty so the inode gets pushed around on the
2765 * dirty lists, and so that O_SYNC works as expected
2766 */
2767 mark_inode_dirty(inode);
2768 reiserfs_update_sd(&myth, inode);
2769 update_sd = 1;
2770 ret = journal_end(&myth, inode->i_sb, 1);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002771 if (ret)
2772 goto journal_error;
2773 }
2774 if (th) {
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002775 if (!locked) {
2776 lock_depth = reiserfs_write_lock_once(inode->i_sb);
2777 locked = true;
2778 }
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002779 if (!update_sd)
2780 mark_inode_dirty(inode);
2781 ret = reiserfs_end_persistent_transaction(th);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002782 if (ret)
2783 goto out;
2784 }
2785
2786 out:
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002787 if (locked)
2788 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002789 unlock_page(page);
2790 page_cache_release(page);
Jan Karaec8e2f72009-12-17 15:27:06 -08002791
2792 if (pos + len > inode->i_size)
2793 reiserfs_truncate_failed_write(inode);
2794
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002795 return ret == 0 ? copied : ret;
2796
2797 journal_error:
Frederic Weisbeckerd6f5b0a2009-05-08 14:53:52 +02002798 reiserfs_write_unlock_once(inode->i_sb, lock_depth);
2799 locked = false;
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002800 if (th) {
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002801 if (!update_sd)
2802 reiserfs_update_sd(th, inode);
2803 ret = reiserfs_end_persistent_transaction(th);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002804 }
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07002805 goto out;
2806}
2807
2808int reiserfs_commit_write(struct file *f, struct page *page,
2809 unsigned from, unsigned to)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002810{
2811 struct inode *inode = page->mapping->host;
2812 loff_t pos = ((loff_t) page->index << PAGE_CACHE_SHIFT) + to;
2813 int ret = 0;
2814 int update_sd = 0;
2815 struct reiserfs_transaction_handle *th = NULL;
2816
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002817 reiserfs_write_unlock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002818 reiserfs_wait_on_write_block(inode->i_sb);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002819 reiserfs_write_lock(inode->i_sb);
2820
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002821 if (reiserfs_transaction_running(inode->i_sb)) {
2822 th = current->journal_info;
2823 }
2824 reiserfs_commit_page(inode, page, from, to);
2825
2826 /* generic_commit_write does this for us, but does not update the
2827 ** transaction tracking stuff when the size changes. So, we have
2828 ** to do the i_size updates here.
2829 */
2830 if (pos > inode->i_size) {
2831 struct reiserfs_transaction_handle myth;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002832 /* If the file have grown beyond the border where it
2833 can have a tail, unmark it as needing a tail
2834 packing */
2835 if ((have_large_tails(inode->i_sb)
2836 && inode->i_size > i_block_size(inode) * 4)
2837 || (have_small_tails(inode->i_sb)
2838 && inode->i_size > i_block_size(inode)))
2839 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
2840
2841 ret = journal_begin(&myth, inode->i_sb, 1);
Frederic Weisbecker7e942772009-08-25 03:38:12 +02002842 if (ret)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002843 goto journal_error;
Frederic Weisbecker7e942772009-08-25 03:38:12 +02002844
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002845 reiserfs_update_inode_transaction(inode);
2846 inode->i_size = pos;
Chris Mason9f037832005-09-13 01:25:17 -07002847 /*
2848 * this will just nest into our transaction. It's important
2849 * to use mark_inode_dirty so the inode gets pushed around on the
2850 * dirty lists, and so that O_SYNC works as expected
2851 */
2852 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002853 reiserfs_update_sd(&myth, inode);
2854 update_sd = 1;
2855 ret = journal_end(&myth, inode->i_sb, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002856 if (ret)
2857 goto journal_error;
2858 }
2859 if (th) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002860 if (!update_sd)
Chris Mason9f037832005-09-13 01:25:17 -07002861 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002862 ret = reiserfs_end_persistent_transaction(th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002863 if (ret)
2864 goto out;
2865 }
2866
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002867 out:
2868 return ret;
2869
2870 journal_error:
2871 if (th) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002872 if (!update_sd)
2873 reiserfs_update_sd(th, inode);
2874 ret = reiserfs_end_persistent_transaction(th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002875 }
2876
2877 return ret;
2878}
2879
2880void sd_attrs_to_i_attrs(__u16 sd_attrs, struct inode *inode)
2881{
2882 if (reiserfs_attrs(inode->i_sb)) {
2883 if (sd_attrs & REISERFS_SYNC_FL)
2884 inode->i_flags |= S_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002886 inode->i_flags &= ~S_SYNC;
2887 if (sd_attrs & REISERFS_IMMUTABLE_FL)
2888 inode->i_flags |= S_IMMUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002890 inode->i_flags &= ~S_IMMUTABLE;
2891 if (sd_attrs & REISERFS_APPEND_FL)
2892 inode->i_flags |= S_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002894 inode->i_flags &= ~S_APPEND;
2895 if (sd_attrs & REISERFS_NOATIME_FL)
2896 inode->i_flags |= S_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002898 inode->i_flags &= ~S_NOATIME;
2899 if (sd_attrs & REISERFS_NOTAIL_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 REISERFS_I(inode)->i_flags |= i_nopack_mask;
2901 else
2902 REISERFS_I(inode)->i_flags &= ~i_nopack_mask;
2903 }
2904}
2905
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002906void i_attrs_to_sd_attrs(struct inode *inode, __u16 * sd_attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002908 if (reiserfs_attrs(inode->i_sb)) {
2909 if (inode->i_flags & S_IMMUTABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 *sd_attrs |= REISERFS_IMMUTABLE_FL;
2911 else
2912 *sd_attrs &= ~REISERFS_IMMUTABLE_FL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002913 if (inode->i_flags & S_SYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 *sd_attrs |= REISERFS_SYNC_FL;
2915 else
2916 *sd_attrs &= ~REISERFS_SYNC_FL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002917 if (inode->i_flags & S_NOATIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 *sd_attrs |= REISERFS_NOATIME_FL;
2919 else
2920 *sd_attrs &= ~REISERFS_NOATIME_FL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002921 if (REISERFS_I(inode)->i_flags & i_nopack_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 *sd_attrs |= REISERFS_NOTAIL_FL;
2923 else
2924 *sd_attrs &= ~REISERFS_NOTAIL_FL;
2925 }
2926}
2927
2928/* decide if this buffer needs to stay around for data logging or ordered
2929** write purposes
2930*/
2931static int invalidatepage_can_drop(struct inode *inode, struct buffer_head *bh)
2932{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002933 int ret = 1;
2934 struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Chris Masond62b1b82006-02-01 03:06:47 -08002936 lock_buffer(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002937 spin_lock(&j->j_dirty_buffers_lock);
2938 if (!buffer_mapped(bh)) {
2939 goto free_jh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002941 /* the page is locked, and the only places that log a data buffer
2942 * also lock the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002944 if (reiserfs_file_data_log(inode)) {
2945 /*
2946 * very conservative, leave the buffer pinned if
2947 * anyone might need it.
2948 */
2949 if (buffer_journaled(bh) || buffer_journal_dirty(bh)) {
2950 ret = 0;
2951 }
Chris Masond62b1b82006-02-01 03:06:47 -08002952 } else if (buffer_dirty(bh)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002953 struct reiserfs_journal_list *jl;
2954 struct reiserfs_jh *jh = bh->b_private;
2955
2956 /* why is this safe?
2957 * reiserfs_setattr updates i_size in the on disk
2958 * stat data before allowing vmtruncate to be called.
2959 *
2960 * If buffer was put onto the ordered list for this
2961 * transaction, we know for sure either this transaction
2962 * or an older one already has updated i_size on disk,
2963 * and this ordered data won't be referenced in the file
2964 * if we crash.
2965 *
2966 * if the buffer was put onto the ordered list for an older
2967 * transaction, we need to leave it around
2968 */
2969 if (jh && (jl = jh->jl)
2970 && jl != SB_JOURNAL(inode->i_sb)->j_current_jl)
2971 ret = 0;
2972 }
2973 free_jh:
2974 if (ret && bh->b_private) {
2975 reiserfs_free_jh(bh);
2976 }
2977 spin_unlock(&j->j_dirty_buffers_lock);
Chris Masond62b1b82006-02-01 03:06:47 -08002978 unlock_buffer(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002979 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
2982/* clm -- taken from fs/buffer.c:block_invalidate_page */
Lukas Czernerd47992f2013-05-21 23:17:23 -04002983static void reiserfs_invalidatepage(struct page *page, unsigned int offset,
2984 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002986 struct buffer_head *head, *bh, *next;
2987 struct inode *inode = page->mapping->host;
2988 unsigned int curr_off = 0;
Lukas Czernerbad54832013-05-21 23:58:51 -04002989 unsigned int stop = offset + length;
2990 int partial_page = (offset || length < PAGE_CACHE_SIZE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002991 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002993 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Lukas Czernerbad54832013-05-21 23:58:51 -04002995 if (!partial_page)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002996 ClearPageChecked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002998 if (!page_has_buffers(page))
2999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003001 head = page_buffers(page);
3002 bh = head;
3003 do {
3004 unsigned int next_off = curr_off + bh->b_size;
3005 next = bh->b_this_page;
3006
Lukas Czernerbad54832013-05-21 23:58:51 -04003007 if (next_off > stop)
3008 goto out;
3009
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003010 /*
3011 * is this block fully invalidated?
3012 */
3013 if (offset <= curr_off) {
3014 if (invalidatepage_can_drop(inode, bh))
3015 reiserfs_unmap_buffer(bh);
3016 else
3017 ret = 0;
3018 }
3019 curr_off = next_off;
3020 bh = next;
3021 } while (bh != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 /*
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003024 * We release buffers only if the entire page is being invalidated.
3025 * The get_block cached value has been unconditionally invalidated,
3026 * so real IO is not possible anymore.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 */
Lukas Czernerbad54832013-05-21 23:58:51 -04003028 if (!partial_page && ret) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003029 ret = try_to_release_page(page, 0);
NeilBrown2ff28e22006-03-26 01:37:18 -08003030 /* maybe should BUG_ON(!ret); - neilb */
3031 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003032 out:
NeilBrown2ff28e22006-03-26 01:37:18 -08003033 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034}
3035
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003036static int reiserfs_set_page_dirty(struct page *page)
3037{
3038 struct inode *inode = page->mapping->host;
3039 if (reiserfs_file_data_log(inode)) {
3040 SetPageChecked(page);
3041 return __set_page_dirty_nobuffers(page);
3042 }
3043 return __set_page_dirty_buffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
3046/*
3047 * Returns 1 if the page's buffers were dropped. The page is locked.
3048 *
3049 * Takes j_dirty_buffers_lock to protect the b_assoc_buffers list_heads
3050 * in the buffers at page_buffers(page).
3051 *
3052 * even in -o notail mode, we can't be sure an old mount without -o notail
3053 * didn't create files with tails.
3054 */
Al Viro27496a82005-10-21 03:20:48 -04003055static int reiserfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003057 struct inode *inode = page->mapping->host;
3058 struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
3059 struct buffer_head *head;
3060 struct buffer_head *bh;
3061 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003063 WARN_ON(PageChecked(page));
3064 spin_lock(&j->j_dirty_buffers_lock);
3065 head = page_buffers(page);
3066 bh = head;
3067 do {
3068 if (bh->b_private) {
3069 if (!buffer_dirty(bh) && !buffer_locked(bh)) {
3070 reiserfs_free_jh(bh);
3071 } else {
3072 ret = 0;
3073 break;
3074 }
3075 }
3076 bh = bh->b_this_page;
3077 } while (bh != head);
3078 if (ret)
3079 ret = try_to_free_buffers(page);
3080 spin_unlock(&j->j_dirty_buffers_lock);
3081 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
3084/* We thank Mingming Cao for helping us understand in great detail what
3085 to do in this section of the code. */
3086static ssize_t reiserfs_direct_IO(int rw, struct kiocb *iocb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003087 const struct iovec *iov, loff_t offset,
3088 unsigned long nr_segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003090 struct file *file = iocb->ki_filp;
3091 struct inode *inode = file->f_mapping->host;
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003092 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Christoph Hellwigaacfc19c2011-06-24 14:29:47 -04003094 ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
3095 reiserfs_get_blocks_direct_io);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003096
3097 /*
3098 * In case of error extending write may have instantiated a few
3099 * blocks outside i_size. Trim these off again.
3100 */
3101 if (unlikely((rw & WRITE) && ret < 0)) {
3102 loff_t isize = i_size_read(inode);
3103 loff_t end = offset + iov_length(iov, nr_segs);
3104
Marco Stornellicfac4b42012-12-15 11:47:31 +01003105 if ((end > isize) && inode_newsize_ok(inode, isize) == 0) {
3106 truncate_setsize(inode, isize);
3107 reiserfs_vfs_truncate_file(inode);
3108 }
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02003109 }
3110
3111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
3113
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003114int reiserfs_setattr(struct dentry *dentry, struct iattr *attr)
3115{
3116 struct inode *inode = dentry->d_inode;
Jeff Laytoncdd6fe62007-10-18 03:05:19 -07003117 unsigned int ia_valid;
Frederic Weisbecker5fe1533f2010-01-04 22:04:01 +01003118 int depth;
3119 int error;
Jeff Laytoncdd6fe62007-10-18 03:05:19 -07003120
Christoph Hellwigdb78b872010-06-04 11:30:03 +02003121 error = inode_change_ok(inode, attr);
3122 if (error)
3123 return error;
3124
Jeff Laytoncdd6fe62007-10-18 03:05:19 -07003125 /* must be turned off for recursive notify_change calls */
3126 ia_valid = attr->ia_valid &= ~(ATTR_KILL_SUID|ATTR_KILL_SGID);
3127
Dmitry Monakhov12755622010-04-08 22:04:20 +04003128 if (is_quota_modification(inode, attr))
Christoph Hellwig871a2932010-03-03 09:05:07 -05003129 dquot_initialize(inode);
Jan Kara7af11682012-11-13 17:05:14 +01003130 depth = reiserfs_write_lock_once(inode->i_sb);
Dmitry Monakhov12755622010-04-08 22:04:20 +04003131 if (attr->ia_valid & ATTR_SIZE) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003132 /* version 2 items will be caught by the s_maxbytes check
3133 ** done for us in vmtruncate
3134 */
3135 if (get_inode_item_key_version(inode) == KEY_FORMAT_3_5 &&
3136 attr->ia_size > MAX_NON_LFS) {
Jeff Mahoney4c051412013-08-08 17:27:19 -04003137 reiserfs_write_unlock_once(inode->i_sb, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003138 error = -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
Christoph Hellwig562c72aa52011-06-24 14:29:45 -04003141
3142 inode_dio_wait(inode);
3143
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003144 /* fill in hole pointers in the expanding truncate case. */
3145 if (attr->ia_size > inode->i_size) {
Vladimir Savelievf7557e82007-10-16 01:25:14 -07003146 error = generic_cont_expand_simple(inode, attr->ia_size);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003147 if (REISERFS_I(inode)->i_prealloc_count > 0) {
3148 int err;
3149 struct reiserfs_transaction_handle th;
3150 /* we're changing at most 2 bitmaps, inode + super */
3151 err = journal_begin(&th, inode->i_sb, 4);
3152 if (!err) {
3153 reiserfs_discard_prealloc(&th, inode);
3154 err = journal_end(&th, inode->i_sb, 4);
3155 }
3156 if (err)
3157 error = err;
3158 }
Jeff Mahoney4c051412013-08-08 17:27:19 -04003159 if (error) {
3160 reiserfs_write_unlock_once(inode->i_sb, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003161 goto out;
Jeff Mahoney4c051412013-08-08 17:27:19 -04003162 }
Vladimir Savelievdd535a52006-07-01 04:36:32 -07003163 /*
3164 * file size is changed, ctime and mtime are
3165 * to be updated
3166 */
3167 attr->ia_valid |= (ATTR_MTIME | ATTR_CTIME);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003168 }
3169 }
Jeff Mahoney4c051412013-08-08 17:27:19 -04003170 reiserfs_write_unlock_once(inode->i_sb, depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Eric W. Biedermandf814652012-02-07 16:30:06 -08003172 if ((((attr->ia_valid & ATTR_UID) && (from_kuid(&init_user_ns, attr->ia_uid) & ~0xffff)) ||
3173 ((attr->ia_valid & ATTR_GID) && (from_kgid(&init_user_ns, attr->ia_gid) & ~0xffff))) &&
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003174 (get_inode_sd_version(inode) == STAT_DATA_V1)) {
3175 /* stat data of format v3.5 has 16 bit uid and gid */
3176 error = -EINVAL;
3177 goto out;
3178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Eric W. Biedermandf814652012-02-07 16:30:06 -08003180 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
3181 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Christoph Hellwig10257742010-06-04 11:30:02 +02003182 struct reiserfs_transaction_handle th;
3183 int jbegin_count =
3184 2 *
3185 (REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb) +
3186 REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb)) +
3187 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Christoph Hellwig10257742010-06-04 11:30:02 +02003189 error = reiserfs_chown_xattrs(inode, attr);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003190
Christoph Hellwig10257742010-06-04 11:30:02 +02003191 if (error)
3192 return error;
3193
3194 /* (user+group)*(old+new) structure - we count quota info and , inode write (sb, inode) */
Jeff Mahoney4c051412013-08-08 17:27:19 -04003195 depth = reiserfs_write_lock_once(inode->i_sb);
Christoph Hellwig10257742010-06-04 11:30:02 +02003196 error = journal_begin(&th, inode->i_sb, jbegin_count);
Jeff Mahoney4c051412013-08-08 17:27:19 -04003197 reiserfs_write_unlock_once(inode->i_sb, depth);
Christoph Hellwig10257742010-06-04 11:30:02 +02003198 if (error)
3199 goto out;
3200 error = dquot_transfer(inode, attr);
Jan Kara7af11682012-11-13 17:05:14 +01003201 depth = reiserfs_write_lock_once(inode->i_sb);
Christoph Hellwig10257742010-06-04 11:30:02 +02003202 if (error) {
3203 journal_end(&th, inode->i_sb, jbegin_count);
Jeff Mahoney4c051412013-08-08 17:27:19 -04003204 reiserfs_write_unlock_once(inode->i_sb, depth);
Christoph Hellwig10257742010-06-04 11:30:02 +02003205 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003206 }
Christoph Hellwig10257742010-06-04 11:30:02 +02003207
3208 /* Update corresponding info in inode so that everything is in
3209 * one transaction */
3210 if (attr->ia_valid & ATTR_UID)
3211 inode->i_uid = attr->ia_uid;
3212 if (attr->ia_valid & ATTR_GID)
3213 inode->i_gid = attr->ia_gid;
3214 mark_inode_dirty(inode);
3215 error = journal_end(&th, inode->i_sb, jbegin_count);
Jeff Mahoney4c051412013-08-08 17:27:19 -04003216 reiserfs_write_unlock_once(inode->i_sb, depth);
Christoph Hellwig10257742010-06-04 11:30:02 +02003217 if (error)
3218 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003219 }
3220
Christoph Hellwig10257742010-06-04 11:30:02 +02003221 if ((attr->ia_valid & ATTR_SIZE) &&
Marco Stornellicfac4b42012-12-15 11:47:31 +01003222 attr->ia_size != i_size_read(inode)) {
3223 error = inode_newsize_ok(inode, attr->ia_size);
3224 if (!error) {
3225 truncate_setsize(inode, attr->ia_size);
3226 reiserfs_vfs_truncate_file(inode);
3227 }
3228 }
Christoph Hellwig10257742010-06-04 11:30:02 +02003229
3230 if (!error) {
3231 setattr_copy(inode, attr);
3232 mark_inode_dirty(inode);
3233 }
Christoph Hellwig10257742010-06-04 11:30:02 +02003234
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003235 if (!error && reiserfs_posixacl(inode->i_sb)) {
3236 if (attr->ia_valid & ATTR_MODE)
3237 error = reiserfs_acl_chmod(inode);
3238 }
3239
Jeff Mahoney4c051412013-08-08 17:27:19 -04003240out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003241 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003244const struct address_space_operations reiserfs_address_space_operations = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003245 .writepage = reiserfs_writepage,
3246 .readpage = reiserfs_readpage,
3247 .readpages = reiserfs_readpages,
3248 .releasepage = reiserfs_releasepage,
3249 .invalidatepage = reiserfs_invalidatepage,
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -07003250 .write_begin = reiserfs_write_begin,
3251 .write_end = reiserfs_write_end,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003252 .bmap = reiserfs_aop_bmap,
3253 .direct_IO = reiserfs_direct_IO,
3254 .set_page_dirty = reiserfs_set_page_dirty,
3255};