inode.c 9.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11
/*
 *   Copyright (C) International Business Machines Corp., 2000-2004
 *   Portions Copyright (C) Christoph Hellwig, 2001-2002
 */

#include <linux/fs.h>
#include <linux/mpage.h>
#include <linux/buffer_head.h>
#include <linux/pagemap.h>
#include <linux/quotaops.h>
12
#include <linux/uio.h>
13
#include <linux/writeback.h>
L
Linus Torvalds 已提交
14
#include "jfs_incore.h"
15
#include "jfs_inode.h"
L
Linus Torvalds 已提交
16 17 18 19 20
#include "jfs_filsys.h"
#include "jfs_imap.h"
#include "jfs_extent.h"
#include "jfs_unicode.h"
#include "jfs_debug.h"
A
Al Viro 已提交
21
#include "jfs_dmap.h"
L
Linus Torvalds 已提交
22 23


24
struct inode *jfs_iget(struct super_block *sb, unsigned long ino)
L
Linus Torvalds 已提交
25
{
26 27 28 29 30 31 32 33 34 35 36 37 38
	struct inode *inode;
	int ret;

	inode = iget_locked(sb, ino);
	if (!inode)
		return ERR_PTR(-ENOMEM);
	if (!(inode->i_state & I_NEW))
		return inode;

	ret = diRead(inode);
	if (ret < 0) {
		iget_failed(inode);
		return ERR_PTR(ret);
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46 47 48 49 50
	}

	if (S_ISREG(inode->i_mode)) {
		inode->i_op = &jfs_file_inode_operations;
		inode->i_fop = &jfs_file_operations;
		inode->i_mapping->a_ops = &jfs_aops;
	} else if (S_ISDIR(inode->i_mode)) {
		inode->i_op = &jfs_dir_inode_operations;
		inode->i_fop = &jfs_dir_operations;
	} else if (S_ISLNK(inode->i_mode)) {
		if (inode->i_size >= IDATASIZE) {
			inode->i_op = &page_symlink_inode_operations;
51
			inode_nohighmem(inode);
L
Linus Torvalds 已提交
52
			inode->i_mapping->a_ops = &jfs_aops;
53
		} else {
54
			inode->i_op = &jfs_fast_symlink_inode_operations;
A
Al Viro 已提交
55
			inode->i_link = JFS_IP(inode)->i_inline;
56 57 58 59
			/*
			 * The inline data should be null-terminated, but
			 * don't let on-disk corruption crash the kernel
			 */
A
Al Viro 已提交
60
			inode->i_link[inode->i_size] = '\0';
61
		}
L
Linus Torvalds 已提交
62 63 64 65
	} else {
		inode->i_op = &jfs_file_inode_operations;
		init_special_inode(inode, inode->i_mode, inode->i_rdev);
	}
66 67
	unlock_new_inode(inode);
	return inode;
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}

/*
 * Workhorse of both fsync & write_inode
 */
int jfs_commit_inode(struct inode *inode, int wait)
{
	int rc = 0;
	tid_t tid;
	static int noisy = 5;

	jfs_info("In jfs_commit_inode, inode = 0x%p", inode);

	/*
	 * Don't commit if inode has been committed since last being
	 * marked dirty, or if it has been deleted.
	 */
	if (inode->i_nlink == 0 || !test_cflag(COMMIT_Dirty, inode))
		return 0;

	if (isReadOnly(inode)) {
		/* kernel allows writes to devices on read-only
		 * partitions and may think inode is dirty
		 */
		if (!special_file(inode->i_mode) && noisy) {
J
Joe Perches 已提交
93 94
			jfs_err("jfs_commit_inode(0x%p) called on read-only volume",
				inode);
L
Linus Torvalds 已提交
95 96 97 98 99 100 101
			jfs_err("Is remount racy?");
			noisy--;
		}
		return 0;
	}

	tid = txBegin(inode->i_sb, COMMIT_INODE);
102
	mutex_lock(&JFS_IP(inode)->commit_mutex);
L
Linus Torvalds 已提交
103 104

	/*
105
	 * Retest inode state after taking commit_mutex
L
Linus Torvalds 已提交
106 107 108 109 110
	 */
	if (inode->i_nlink && test_cflag(COMMIT_Dirty, inode))
		rc = txCommit(tid, 1, &inode, wait ? COMMIT_SYNC : 0);

	txEnd(tid);
111
	mutex_unlock(&JFS_IP(inode)->commit_mutex);
L
Linus Torvalds 已提交
112 113 114
	return rc;
}

115
int jfs_write_inode(struct inode *inode, struct writeback_control *wbc)
L
Linus Torvalds 已提交
116
{
117 118
	int wait = wbc->sync_mode == WB_SYNC_ALL;

D
Dave Kleikamp 已提交
119
	if (inode->i_nlink == 0)
L
Linus Torvalds 已提交
120 121 122 123 124 125
		return 0;
	/*
	 * If COMMIT_DIRTY is not set, the inode isn't really dirty.
	 * It has been committed since the last change, but was still
	 * on the dirty inode list.
	 */
126
	if (!test_cflag(COMMIT_Dirty, inode)) {
L
Linus Torvalds 已提交
127 128 129
		/* Make sure committed changes hit the disk */
		jfs_flush_journal(JFS_SBI(inode->i_sb)->log, wait);
		return 0;
130
	}
L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138

	if (jfs_commit_inode(inode, wait)) {
		jfs_err("jfs_write_inode: jfs_commit_inode failed!");
		return -EIO;
	} else
		return 0;
}

A
Al Viro 已提交
139
void jfs_evict_inode(struct inode *inode)
L
Linus Torvalds 已提交
140
{
A
Al Viro 已提交
141 142
	struct jfs_inode_info *ji = JFS_IP(inode);

A
Al Viro 已提交
143
	jfs_info("In jfs_evict_inode, inode = 0x%p", inode);
L
Linus Torvalds 已提交
144

A
Al Viro 已提交
145
	if (!inode->i_nlink && !is_bad_inode(inode)) {
146
		dquot_initialize(inode);
147

A
Al Viro 已提交
148
		if (JFS_IP(inode)->fileset == FILESYSTEM_I) {
149
			truncate_inode_pages_final(&inode->i_data);
150

A
Al Viro 已提交
151 152
			if (test_cflag(COMMIT_Freewmap, inode))
				jfs_free_zero_link(inode);
L
Linus Torvalds 已提交
153

P
Pavel Skripkin 已提交
154 155
			if (JFS_SBI(inode->i_sb)->ipimap)
				diFree(inode);
L
Linus Torvalds 已提交
156

A
Al Viro 已提交
157 158 159 160 161 162
			/*
			 * Free the inode from the quota allocation.
			 */
			dquot_free_inode(inode);
		}
	} else {
163
		truncate_inode_pages_final(&inode->i_data);
164
	}
165
	clear_inode(inode);
A
Al Viro 已提交
166
	dquot_drop(inode);
A
Al Viro 已提交
167 168 169 170 171 172 173 174 175 176

	BUG_ON(!list_empty(&ji->anon_inode_list));

	spin_lock_irq(&ji->ag_lock);
	if (ji->active_ag != -1) {
		struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
		atomic_dec(&bmap->db_active[ji->active_ag]);
		ji->active_ag = -1;
	}
	spin_unlock_irq(&ji->ag_lock);
L
Linus Torvalds 已提交
177 178
}

179
void jfs_dirty_inode(struct inode *inode, int flags)
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
{
	static int noisy = 5;

	if (isReadOnly(inode)) {
		if (!special_file(inode->i_mode) && noisy) {
			/* kernel allows writes to devices on read-only
			 * partitions and may try to mark inode dirty
			 */
			jfs_err("jfs_dirty_inode called on read-only volume");
			jfs_err("Is remount racy?");
			noisy--;
		}
		return;
	}

	set_cflag(COMMIT_Dirty, inode);
}

198 199
int jfs_get_block(struct inode *ip, sector_t lblock,
		  struct buffer_head *bh_result, int create)
L
Linus Torvalds 已提交
200 201 202 203 204 205
{
	s64 lblock64 = lblock;
	int rc = 0;
	xad_t xad;
	s64 xaddr;
	int xflag;
206
	s32 xlen = bh_result->b_size >> ip->i_blkbits;
L
Linus Torvalds 已提交
207 208 209 210

	/*
	 * Take appropriate lock on inode
	 */
211
	if (create)
D
Dave Kleikamp 已提交
212
		IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
213
	else
D
Dave Kleikamp 已提交
214
		IREAD_LOCK(ip, RDWRLOCK_NORMAL);
L
Linus Torvalds 已提交
215 216

	if (((lblock64 << ip->i_sb->s_blocksize_bits) < ip->i_size) &&
217
	    (!xtLookup(ip, lblock64, xlen, &xflag, &xaddr, &xlen, 0)) &&
218
	    xaddr) {
L
Linus Torvalds 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
		if (xflag & XAD_NOTRECORDED) {
			if (!create)
				/*
				 * Allocated but not recorded, read treats
				 * this as a hole
				 */
				goto unlock;
#ifdef _JFS_4K
			XADoffset(&xad, lblock64);
			XADlength(&xad, xlen);
			XADaddress(&xad, xaddr);
#else				/* _JFS_4K */
			/*
			 * As long as block size = 4K, this isn't a problem.
			 * We should mark the whole page not ABNR, but how
			 * will we know to mark the other blocks BH_New?
			 */
			BUG();
#endif				/* _JFS_4K */
			rc = extRecord(ip, &xad);
			if (rc)
				goto unlock;
			set_buffer_new(bh_result);
		}

		map_bh(bh_result, ip->i_sb, xaddr);
		bh_result->b_size = xlen << ip->i_blkbits;
		goto unlock;
	}
	if (!create)
		goto unlock;

	/*
	 * Allocate a new block
	 */
#ifdef _JFS_4K
	if ((rc = extHint(ip, lblock64 << ip->i_sb->s_blocksize_bits, &xad)))
		goto unlock;
257
	rc = extAlloc(ip, xlen, lblock64, &xad, false);
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	if (rc)
		goto unlock;

	set_buffer_new(bh_result);
	map_bh(bh_result, ip->i_sb, addressXAD(&xad));
	bh_result->b_size = lengthXAD(&xad) << ip->i_blkbits;

#else				/* _JFS_4K */
	/*
	 * We need to do whatever it takes to keep all but the last buffers
	 * in 4K pages - see jfs_write.c
	 */
	BUG();
#endif				/* _JFS_4K */

      unlock:
	/*
	 * Release lock on inode
	 */
277 278 279 280
	if (create)
		IWRITE_UNLOCK(ip);
	else
		IREAD_UNLOCK(ip);
L
Linus Torvalds 已提交
281 282 283 284 285
	return rc;
}

static int jfs_writepage(struct page *page, struct writeback_control *wbc)
{
N
Nick Piggin 已提交
286
	return block_write_full_page(page, jfs_get_block, wbc);
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299
}

static int jfs_writepages(struct address_space *mapping,
			struct writeback_control *wbc)
{
	return mpage_writepages(mapping, wbc, jfs_get_block);
}

static int jfs_readpage(struct file *file, struct page *page)
{
	return mpage_readpage(page, jfs_get_block);
}

300
static void jfs_readahead(struct readahead_control *rac)
L
Linus Torvalds 已提交
301
{
302
	mpage_readahead(rac, jfs_get_block);
L
Linus Torvalds 已提交
303 304
}

M
Marco Stornelli 已提交
305 306 307 308 309
static void jfs_write_failed(struct address_space *mapping, loff_t to)
{
	struct inode *inode = mapping->host;

	if (to > inode->i_size) {
310
		truncate_pagecache(inode, inode->i_size);
M
Marco Stornelli 已提交
311 312 313 314
		jfs_truncate(inode);
	}
}

N
Nick Piggin 已提交
315 316 317
static int jfs_write_begin(struct file *file, struct address_space *mapping,
				loff_t pos, unsigned len, unsigned flags,
				struct page **pagep, void **fsdata)
L
Linus Torvalds 已提交
318
{
319 320 321
	int ret;

	ret = nobh_write_begin(mapping, pos, len, flags, pagep, fsdata,
N
Nick Piggin 已提交
322
				jfs_get_block);
M
Marco Stornelli 已提交
323 324
	if (unlikely(ret))
		jfs_write_failed(mapping, pos + len);
325 326

	return ret;
L
Linus Torvalds 已提交
327 328 329 330 331 332 333
}

static sector_t jfs_bmap(struct address_space *mapping, sector_t block)
{
	return generic_block_bmap(mapping, block, jfs_get_block);
}

334
static ssize_t jfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
L
Linus Torvalds 已提交
335 336
{
	struct file *file = iocb->ki_filp;
M
Marco Stornelli 已提交
337
	struct address_space *mapping = file->f_mapping;
L
Linus Torvalds 已提交
338
	struct inode *inode = file->f_mapping->host;
339
	size_t count = iov_iter_count(iter);
340
	ssize_t ret;
L
Linus Torvalds 已提交
341

342
	ret = blockdev_direct_IO(iocb, inode, iter, jfs_get_block);
343 344 345 346 347

	/*
	 * In case of error extending write may have instantiated a few
	 * blocks outside i_size. Trim these off again.
	 */
348
	if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
349
		loff_t isize = i_size_read(inode);
350
		loff_t end = iocb->ki_pos + count;
351 352

		if (end > isize)
M
Marco Stornelli 已提交
353
			jfs_write_failed(mapping, end);
354 355 356
	}

	return ret;
L
Linus Torvalds 已提交
357 358
}

359
const struct address_space_operations jfs_aops = {
360
	.set_page_dirty	= __set_page_dirty_buffers,
L
Linus Torvalds 已提交
361
	.readpage	= jfs_readpage,
362
	.readahead	= jfs_readahead,
L
Linus Torvalds 已提交
363 364
	.writepage	= jfs_writepage,
	.writepages	= jfs_writepages,
N
Nick Piggin 已提交
365
	.write_begin	= jfs_write_begin,
N
Nick Piggin 已提交
366
	.write_end	= nobh_write_end,
L
Linus Torvalds 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
	.bmap		= jfs_bmap,
	.direct_IO	= jfs_direct_IO,
};

/*
 * Guts of jfs_truncate.  Called with locks already held.  Can be called
 * with directory for truncating directory index table.
 */
void jfs_truncate_nolock(struct inode *ip, loff_t length)
{
	loff_t newsize;
	tid_t tid;

	ASSERT(length >= 0);

	if (test_cflag(COMMIT_Nolink, ip)) {
		xtTruncate(0, ip, length, COMMIT_WMAP);
		return;
	}

	do {
		tid = txBegin(ip->i_sb, 0);

		/*
391
		 * The commit_mutex cannot be taken before txBegin.
L
Linus Torvalds 已提交
392 393 394 395
		 * txBegin may block and there is a chance the inode
		 * could be marked dirty and need to be committed
		 * before txBegin unblocks
		 */
396
		mutex_lock(&JFS_IP(ip)->commit_mutex);
L
Linus Torvalds 已提交
397 398 399 400 401

		newsize = xtTruncate(tid, ip, length,
				     COMMIT_TRUNCATE | COMMIT_PWMAP);
		if (newsize < 0) {
			txEnd(tid);
402
			mutex_unlock(&JFS_IP(ip)->commit_mutex);
L
Linus Torvalds 已提交
403 404 405
			break;
		}

406
		ip->i_mtime = ip->i_ctime = current_time(ip);
L
Linus Torvalds 已提交
407 408 409 410
		mark_inode_dirty(ip);

		txCommit(tid, 1, &ip, 0);
		txEnd(tid);
411
		mutex_unlock(&JFS_IP(ip)->commit_mutex);
L
Linus Torvalds 已提交
412 413 414 415 416 417 418
	} while (newsize > length);	/* Truncate isn't always atomic */
}

void jfs_truncate(struct inode *ip)
{
	jfs_info("jfs_truncate: size = 0x%lx", (ulong) ip->i_size);

N
Nick Piggin 已提交
419
	nobh_truncate_page(ip->i_mapping, ip->i_size, jfs_get_block);
L
Linus Torvalds 已提交
420

D
Dave Kleikamp 已提交
421
	IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
L
Linus Torvalds 已提交
422 423 424
	jfs_truncate_nolock(ip, ip->i_size);
	IWRITE_UNLOCK(ip);
}