file.c 7.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * file.c
 *
 * PURPOSE
 *  File handling routines for the OSTA-UDF(tm) filesystem.
 *
 * COPYRIGHT
 *  This file is distributed under the terms of the GNU General Public
 *  License (GPL). Copies of the GPL can be obtained from:
 *    ftp://prep.ai.mit.edu/pub/gnu/GPL
 *  Each contributing author retains all rights to their own work.
 *
 *  (C) 1998-1999 Dave Boynton
 *  (C) 1998-2004 Ben Fennema
 *  (C) 1999-2000 Stelias Computing Inc
 *
 * HISTORY
 *
 *  10/02/98 dgb  Attempt to integrate into udf.o
 *  10/07/98      Switched to using generic_readpage, etc., like isofs
 *                And it works!
 *  12/06/98 blf  Added udf_file_read. uses generic_file_read for all cases but
 *                ICBTAG_FLAG_AD_IN_ICB.
 *  04/06/99      64 bit file handling on 32 bit systems taken from ext2 file.c
 *  05/12/99      Preliminary file write support
 */

#include "udfdecl.h"
#include <linux/fs.h>
Fabian Frederick's avatar
Fabian Frederick committed
30
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
31
#include <linux/kernel.h>
32
#include <linux/string.h> /* memset */
33
#include <linux/capability.h>
Linus Torvalds's avatar
Linus Torvalds committed
34 35
#include <linux/errno.h>
#include <linux/pagemap.h>
36
#include <linux/uio.h>
Linus Torvalds's avatar
Linus Torvalds committed
37 38 39 40

#include "udf_i.h"
#include "udf_sb.h"

41
static void __udf_adinicb_readpage(struct page *page)
Linus Torvalds's avatar
Linus Torvalds committed
42 43 44
{
	struct inode *inode = page->mapping->host;
	char *kaddr;
45
	struct udf_inode_info *iinfo = UDF_I(inode);
46
	loff_t isize = i_size_read(inode);
Linus Torvalds's avatar
Linus Torvalds committed
47

48 49 50 51
	/*
	 * We have to be careful here as truncate can change i_size under us.
	 * So just sample it once and use the same value everywhere.
	 */
52
	kaddr = kmap_atomic(page);
53
	memcpy(kaddr, iinfo->i_data + iinfo->i_lenEAttr, isize);
54
	memset(kaddr + isize, 0, PAGE_SIZE - isize);
Linus Torvalds's avatar
Linus Torvalds committed
55 56
	flush_dcache_page(page);
	SetPageUptodate(page);
57
	kunmap_atomic(kaddr);
58 59
}

60
static int udf_adinicb_read_folio(struct file *file, struct folio *folio)
61
{
62 63 64
	BUG_ON(!folio_test_locked(folio));
	__udf_adinicb_readpage(&folio->page);
	folio_unlock(folio);
65

Linus Torvalds's avatar
Linus Torvalds committed
66 67 68
	return 0;
}

Marcin Slusarz's avatar
Marcin Slusarz committed
69 70
static int udf_adinicb_writepage(struct page *page,
				 struct writeback_control *wbc)
Linus Torvalds's avatar
Linus Torvalds committed
71 72 73
{
	struct inode *inode = page->mapping->host;
	char *kaddr;
74
	struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds's avatar
Linus Torvalds committed
75

Matt Mackall's avatar
Matt Mackall committed
76
	BUG_ON(!PageLocked(page));
Linus Torvalds's avatar
Linus Torvalds committed
77

78
	kaddr = kmap_atomic(page);
79
	memcpy(iinfo->i_data + iinfo->i_lenEAttr, kaddr, i_size_read(inode));
Linus Torvalds's avatar
Linus Torvalds committed
80
	SetPageUptodate(page);
81 82
	kunmap_atomic(kaddr);
	mark_inode_dirty(inode);
Linus Torvalds's avatar
Linus Torvalds committed
83
	unlock_page(page);
84

Linus Torvalds's avatar
Linus Torvalds committed
85 86 87
	return 0;
}

88 89
static int udf_adinicb_write_begin(struct file *file,
			struct address_space *mapping, loff_t pos,
90
			unsigned len, struct page **pagep,
91 92 93 94
			void **fsdata)
{
	struct page *page;

95
	if (WARN_ON_ONCE(pos >= PAGE_SIZE))
96
		return -EIO;
97
	page = grab_cache_page_write_begin(mapping, 0);
98 99 100 101
	if (!page)
		return -ENOMEM;
	*pagep = page;

102
	if (!PageUptodate(page))
103 104 105 106
		__udf_adinicb_readpage(page);
	return 0;
}

107
static ssize_t udf_adinicb_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Ian Abbott's avatar
Ian Abbott committed
108 109 110 111 112
{
	/* Fallback to buffered I/O. */
	return 0;
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126
static int udf_adinicb_write_end(struct file *file, struct address_space *mapping,
				 loff_t pos, unsigned len, unsigned copied,
				 struct page *page, void *fsdata)
{
	struct inode *inode = page->mapping->host;
	loff_t last_pos = pos + copied;
	if (last_pos > inode->i_size)
		i_size_write(inode, last_pos);
	set_page_dirty(page);
	unlock_page(page);
	put_page(page);
	return copied;
}

127
const struct address_space_operations udf_adinicb_aops = {
128
	.dirty_folio	= block_dirty_folio,
129
	.invalidate_folio = block_invalidate_folio,
130
	.read_folio	= udf_adinicb_read_folio,
131
	.writepage	= udf_adinicb_writepage,
132
	.write_begin	= udf_adinicb_write_begin,
133
	.write_end	= udf_adinicb_write_end,
Ian Abbott's avatar
Ian Abbott committed
134
	.direct_IO	= udf_adinicb_direct_IO,
Linus Torvalds's avatar
Linus Torvalds committed
135 136
};

Al Viro's avatar
Al Viro committed
137
static ssize_t udf_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds's avatar
Linus Torvalds committed
138 139
{
	ssize_t retval;
140
	struct file *file = iocb->ki_filp;
Al Viro's avatar
Al Viro committed
141
	struct inode *inode = file_inode(file);
142
	struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds's avatar
Linus Torvalds committed
143

Al Viro's avatar
Al Viro committed
144
	inode_lock(inode);
145

146 147
	retval = generic_write_checks(iocb, from);
	if (retval <= 0)
148 149
		goto out;

150 151 152
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
	    inode->i_sb->s_blocksize < (udf_file_entry_alloc_offset(inode) +
				 iocb->ki_pos + iov_iter_count(from))) {
153 154 155
		retval = udf_expand_file_adinicb(inode);
		if (retval)
			goto out;
156
	}
Linus Torvalds's avatar
Linus Torvalds committed
157

Al Viro's avatar
Al Viro committed
158
	retval = __generic_file_write_iter(iocb, from);
159
out:
160 161
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB && retval > 0) {
		down_write(&iinfo->i_data_sem);
162
		iinfo->i_lenAlloc = inode->i_size;
163 164
		up_write(&iinfo->i_data_sem);
	}
Al Viro's avatar
Al Viro committed
165
	inode_unlock(inode);
166 167

	if (retval > 0) {
Linus Torvalds's avatar
Linus Torvalds committed
168
		mark_inode_dirty(inode);
169
		retval = generic_write_sync(iocb, retval);
170
	}
171

Linus Torvalds's avatar
Linus Torvalds committed
172 173 174
	return retval;
}

John Kacur's avatar
John Kacur committed
175
long udf_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
176
{
Al Viro's avatar
Al Viro committed
177
	struct inode *inode = file_inode(filp);
178
	long old_block, new_block;
179
	int result;
Linus Torvalds's avatar
Linus Torvalds committed
180

181
	if (file_permission(filp, MAY_READ) != 0) {
John Kacur's avatar
John Kacur committed
182
		udf_debug("no permission to access inode %lu\n", inode->i_ino);
183
		return -EPERM;
Linus Torvalds's avatar
Linus Torvalds committed
184 185
	}

Fabian Frederick's avatar
Fabian Frederick committed
186 187
	if (!arg && ((cmd == UDF_GETVOLIDENT) || (cmd == UDF_GETEASIZE) ||
		     (cmd == UDF_RELOCATE_BLOCKS) || (cmd == UDF_GETEABLOCK))) {
Linus Torvalds's avatar
Linus Torvalds committed
188
		udf_debug("invalid argument to udf_ioctl\n");
189
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
190 191
	}

192 193
	switch (cmd) {
	case UDF_GETVOLIDENT:
Marcin Slusarz's avatar
Marcin Slusarz committed
194 195
		if (copy_to_user((char __user *)arg,
				 UDF_SB(inode->i_sb)->s_volume_ident, 32))
196 197
			return -EFAULT;
		return 0;
198
	case UDF_RELOCATE_BLOCKS:
199 200 201 202
		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		if (get_user(old_block, (long __user *)arg))
			return -EFAULT;
Marcin Slusarz's avatar
Marcin Slusarz committed
203 204 205
		result = udf_relocate_blocks(inode->i_sb,
						old_block, &new_block);
		if (result == 0)
206
			result = put_user(new_block, (long __user *)arg);
207
		return result;
208
	case UDF_GETEASIZE:
209
		return put_user(UDF_I(inode)->i_lenEAttr, (int __user *)arg);
210
	case UDF_GETEABLOCK:
211
		return copy_to_user((char __user *)arg,
212
				    UDF_I(inode)->i_data,
213
				    UDF_I(inode)->i_lenEAttr) ? -EFAULT : 0;
Fabian Frederick's avatar
Fabian Frederick committed
214 215
	default:
		return -ENOIOCTLCMD;
Linus Torvalds's avatar
Linus Torvalds committed
216 217
	}

218
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
219 220
}

221
static int udf_release_file(struct inode *inode, struct file *filp)
Linus Torvalds's avatar
Linus Torvalds committed
222
{
223
	if (filp->f_mode & FMODE_WRITE &&
224
	    atomic_read(&inode->i_writecount) == 1) {
225 226 227 228
		/*
		 * Grab i_mutex to avoid races with writes changing i_size
		 * while we are running.
		 */
Al Viro's avatar
Al Viro committed
229
		inode_lock(inode);
230
		down_write(&UDF_I(inode)->i_data_sem);
Linus Torvalds's avatar
Linus Torvalds committed
231
		udf_discard_prealloc(inode);
Jan Kara's avatar
Jan Kara committed
232
		udf_truncate_tail_extent(inode);
233
		up_write(&UDF_I(inode)->i_data_sem);
Al Viro's avatar
Al Viro committed
234
		inode_unlock(inode);
Linus Torvalds's avatar
Linus Torvalds committed
235 236 237 238
	}
	return 0;
}

239
const struct file_operations udf_file_operations = {
240
	.read_iter		= generic_file_read_iter,
John Kacur's avatar
John Kacur committed
241
	.unlocked_ioctl		= udf_ioctl,
Jan Kara's avatar
Jan Kara committed
242
	.open			= generic_file_open,
243
	.mmap			= generic_file_mmap,
Al Viro's avatar
Al Viro committed
244
	.write_iter		= udf_file_write_iter,
245
	.release		= udf_release_file,
246
	.fsync			= generic_file_fsync,
247
	.splice_read		= generic_file_splice_read,
Jan Kara's avatar
Jan Kara committed
248
	.splice_write		= iter_file_splice_write,
Christoph Hellwig's avatar
Christoph Hellwig committed
249
	.llseek			= generic_file_llseek,
Linus Torvalds's avatar
Linus Torvalds committed
250 251
};

252 253
static int udf_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
		       struct iattr *attr)
254
{
255
	struct inode *inode = d_inode(dentry);
256
	struct super_block *sb = inode->i_sb;
257 258
	int error;

259
	error = setattr_prepare(&init_user_ns, dentry, attr);
260 261
	if (error)
		return error;
Christoph Hellwig's avatar
Christoph Hellwig committed
262

263 264 265 266 267 268 269 270 271
	if ((attr->ia_valid & ATTR_UID) &&
	    UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET) &&
	    !uid_eq(attr->ia_uid, UDF_SB(sb)->s_uid))
		return -EPERM;
	if ((attr->ia_valid & ATTR_GID) &&
	    UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET) &&
	    !gid_eq(attr->ia_gid, UDF_SB(sb)->s_gid))
		return -EPERM;

Christoph Hellwig's avatar
Christoph Hellwig committed
272 273
	if ((attr->ia_valid & ATTR_SIZE) &&
	    attr->ia_size != i_size_read(inode)) {
274
		error = udf_setsize(inode, attr->ia_size);
Christoph Hellwig's avatar
Christoph Hellwig committed
275 276 277 278
		if (error)
			return error;
	}

279 280 281
	if (attr->ia_valid & ATTR_MODE)
		udf_update_extra_perms(inode, attr->ia_mode);

282
	setattr_copy(&init_user_ns, inode, attr);
Christoph Hellwig's avatar
Christoph Hellwig committed
283 284
	mark_inode_dirty(inode);
	return 0;
285 286
}

287
const struct inode_operations udf_file_inode_operations = {
288
	.setattr		= udf_setattr,
Linus Torvalds's avatar
Linus Torvalds committed
289
};