1 /*
2  * file.c
3  *
4  * PURPOSE
5  *  File handling routines for the OSTA-UDF(tm) filesystem.
6  *
7  * COPYRIGHT
8  *  This file is distributed under the terms of the GNU General Public
9  *  License (GPL). Copies of the GPL can be obtained from:
10  *    ftp://prep.ai.mit.edu/pub/gnu/GPL
11  *  Each contributing author retains all rights to their own work.
12  *
13  *  (C) 1998-1999 Dave Boynton
14  *  (C) 1998-2004 Ben Fennema
15  *  (C) 1999-2000 Stelias Computing Inc
16  *
17  * HISTORY
18  *
19  *  10/02/98 dgb  Attempt to integrate into udf.o
20  *  10/07/98      Switched to using generic_readpage, etc., like isofs
21  *                And it works!
22  *  12/06/98 blf  Added udf_file_read. uses generic_file_read for all cases but
23  *                ICBTAG_FLAG_AD_IN_ICB.
24  *  04/06/99      64 bit file handling on 32 bit systems taken from ext2 file.c
25  *  05/12/99      Preliminary file write support
26  */
27 
28 #include "udfdecl.h"
29 #include <linux/fs.h>
30 #include <linux/uaccess.h>
31 #include <linux/kernel.h>
32 #include <linux/string.h> /* memset */
33 #include <linux/capability.h>
34 #include <linux/errno.h>
35 #include <linux/pagemap.h>
36 #include <linux/uio.h>
37 
38 #include "udf_i.h"
39 #include "udf_sb.h"
40 
__udf_adinicb_readpage(struct page * page)41 static void __udf_adinicb_readpage(struct page *page)
42 {
43 	struct inode *inode = page->mapping->host;
44 	char *kaddr;
45 	struct udf_inode_info *iinfo = UDF_I(inode);
46 	loff_t isize = i_size_read(inode);
47 
48 	/*
49 	 * We have to be careful here as truncate can change i_size under us.
50 	 * So just sample it once and use the same value everywhere.
51 	 */
52 	kaddr = kmap_atomic(page);
53 	memcpy(kaddr, iinfo->i_data + iinfo->i_lenEAttr, isize);
54 	memset(kaddr + isize, 0, PAGE_SIZE - isize);
55 	flush_dcache_page(page);
56 	SetPageUptodate(page);
57 	kunmap_atomic(kaddr);
58 }
59 
udf_adinicb_readpage(struct file * file,struct page * page)60 static int udf_adinicb_readpage(struct file *file, struct page *page)
61 {
62 	BUG_ON(!PageLocked(page));
63 	__udf_adinicb_readpage(page);
64 	unlock_page(page);
65 
66 	return 0;
67 }
68 
udf_adinicb_writepage(struct page * page,struct writeback_control * wbc)69 static int udf_adinicb_writepage(struct page *page,
70 				 struct writeback_control *wbc)
71 {
72 	struct inode *inode = page->mapping->host;
73 	char *kaddr;
74 	struct udf_inode_info *iinfo = UDF_I(inode);
75 
76 	BUG_ON(!PageLocked(page));
77 
78 	kaddr = kmap_atomic(page);
79 	memcpy(iinfo->i_data + iinfo->i_lenEAttr, kaddr, i_size_read(inode));
80 	SetPageUptodate(page);
81 	kunmap_atomic(kaddr);
82 	mark_inode_dirty(inode);
83 	unlock_page(page);
84 
85 	return 0;
86 }
87 
udf_adinicb_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata)88 static int udf_adinicb_write_begin(struct file *file,
89 			struct address_space *mapping, loff_t pos,
90 			unsigned len, unsigned flags, struct page **pagep,
91 			void **fsdata)
92 {
93 	struct page *page;
94 
95 	if (WARN_ON_ONCE(pos >= PAGE_SIZE))
96 		return -EIO;
97 	page = grab_cache_page_write_begin(mapping, 0, flags);
98 	if (!page)
99 		return -ENOMEM;
100 	*pagep = page;
101 
102 	if (!PageUptodate(page))
103 		__udf_adinicb_readpage(page);
104 	return 0;
105 }
106 
udf_adinicb_direct_IO(struct kiocb * iocb,struct iov_iter * iter)107 static ssize_t udf_adinicb_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
108 {
109 	/* Fallback to buffered I/O. */
110 	return 0;
111 }
112 
udf_adinicb_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)113 static int udf_adinicb_write_end(struct file *file, struct address_space *mapping,
114 				 loff_t pos, unsigned len, unsigned copied,
115 				 struct page *page, void *fsdata)
116 {
117 	struct inode *inode = page->mapping->host;
118 	loff_t last_pos = pos + copied;
119 	if (last_pos > inode->i_size)
120 		i_size_write(inode, last_pos);
121 	set_page_dirty(page);
122 	unlock_page(page);
123 	put_page(page);
124 	return copied;
125 }
126 
127 const struct address_space_operations udf_adinicb_aops = {
128 	.readpage	= udf_adinicb_readpage,
129 	.writepage	= udf_adinicb_writepage,
130 	.write_begin	= udf_adinicb_write_begin,
131 	.write_end	= udf_adinicb_write_end,
132 	.direct_IO	= udf_adinicb_direct_IO,
133 };
134 
udf_file_write_iter(struct kiocb * iocb,struct iov_iter * from)135 static ssize_t udf_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
136 {
137 	ssize_t retval;
138 	struct file *file = iocb->ki_filp;
139 	struct inode *inode = file_inode(file);
140 	struct udf_inode_info *iinfo = UDF_I(inode);
141 	int err;
142 
143 	inode_lock(inode);
144 
145 	retval = generic_write_checks(iocb, from);
146 	if (retval <= 0)
147 		goto out;
148 
149 	down_write(&iinfo->i_data_sem);
150 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
151 	    inode->i_sb->s_blocksize < (udf_file_entry_alloc_offset(inode) +
152 				 iocb->ki_pos + iov_iter_count(from))) {
153 		err = udf_expand_file_adinicb(inode);
154 		if (err) {
155 			inode_unlock(inode);
156 			udf_debug("udf_expand_adinicb: err=%d\n", err);
157 			return err;
158 		}
159 	} else
160 		up_write(&iinfo->i_data_sem);
161 
162 	retval = __generic_file_write_iter(iocb, from);
163 out:
164 	down_write(&iinfo->i_data_sem);
165 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB && retval > 0)
166 		iinfo->i_lenAlloc = inode->i_size;
167 	up_write(&iinfo->i_data_sem);
168 	inode_unlock(inode);
169 
170 	if (retval > 0) {
171 		mark_inode_dirty(inode);
172 		retval = generic_write_sync(iocb, retval);
173 	}
174 
175 	return retval;
176 }
177 
udf_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)178 long udf_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
179 {
180 	struct inode *inode = file_inode(filp);
181 	long old_block, new_block;
182 	int result;
183 
184 	if (inode_permission(inode, MAY_READ) != 0) {
185 		udf_debug("no permission to access inode %lu\n", inode->i_ino);
186 		return -EPERM;
187 	}
188 
189 	if (!arg && ((cmd == UDF_GETVOLIDENT) || (cmd == UDF_GETEASIZE) ||
190 		     (cmd == UDF_RELOCATE_BLOCKS) || (cmd == UDF_GETEABLOCK))) {
191 		udf_debug("invalid argument to udf_ioctl\n");
192 		return -EINVAL;
193 	}
194 
195 	switch (cmd) {
196 	case UDF_GETVOLIDENT:
197 		if (copy_to_user((char __user *)arg,
198 				 UDF_SB(inode->i_sb)->s_volume_ident, 32))
199 			return -EFAULT;
200 		return 0;
201 	case UDF_RELOCATE_BLOCKS:
202 		if (!capable(CAP_SYS_ADMIN))
203 			return -EPERM;
204 		if (get_user(old_block, (long __user *)arg))
205 			return -EFAULT;
206 		result = udf_relocate_blocks(inode->i_sb,
207 						old_block, &new_block);
208 		if (result == 0)
209 			result = put_user(new_block, (long __user *)arg);
210 		return result;
211 	case UDF_GETEASIZE:
212 		return put_user(UDF_I(inode)->i_lenEAttr, (int __user *)arg);
213 	case UDF_GETEABLOCK:
214 		return copy_to_user((char __user *)arg,
215 				    UDF_I(inode)->i_data,
216 				    UDF_I(inode)->i_lenEAttr) ? -EFAULT : 0;
217 	default:
218 		return -ENOIOCTLCMD;
219 	}
220 
221 	return 0;
222 }
223 
udf_release_file(struct inode * inode,struct file * filp)224 static int udf_release_file(struct inode *inode, struct file *filp)
225 {
226 	if (filp->f_mode & FMODE_WRITE &&
227 	    atomic_read(&inode->i_writecount) == 1) {
228 		/*
229 		 * Grab i_mutex to avoid races with writes changing i_size
230 		 * while we are running.
231 		 */
232 		inode_lock(inode);
233 		down_write(&UDF_I(inode)->i_data_sem);
234 		udf_discard_prealloc(inode);
235 		udf_truncate_tail_extent(inode);
236 		up_write(&UDF_I(inode)->i_data_sem);
237 		inode_unlock(inode);
238 	}
239 	return 0;
240 }
241 
242 const struct file_operations udf_file_operations = {
243 	.read_iter		= generic_file_read_iter,
244 	.unlocked_ioctl		= udf_ioctl,
245 	.open			= generic_file_open,
246 	.mmap			= generic_file_mmap,
247 	.write_iter		= udf_file_write_iter,
248 	.release		= udf_release_file,
249 	.fsync			= generic_file_fsync,
250 	.splice_read		= generic_file_splice_read,
251 	.llseek			= generic_file_llseek,
252 };
253 
udf_setattr(struct dentry * dentry,struct iattr * attr)254 static int udf_setattr(struct dentry *dentry, struct iattr *attr)
255 {
256 	struct inode *inode = d_inode(dentry);
257 	struct super_block *sb = inode->i_sb;
258 	int error;
259 
260 	error = setattr_prepare(dentry, attr);
261 	if (error)
262 		return error;
263 
264 	if ((attr->ia_valid & ATTR_UID) &&
265 	    UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET) &&
266 	    !uid_eq(attr->ia_uid, UDF_SB(sb)->s_uid))
267 		return -EPERM;
268 	if ((attr->ia_valid & ATTR_GID) &&
269 	    UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET) &&
270 	    !gid_eq(attr->ia_gid, UDF_SB(sb)->s_gid))
271 		return -EPERM;
272 
273 	if ((attr->ia_valid & ATTR_SIZE) &&
274 	    attr->ia_size != i_size_read(inode)) {
275 		error = udf_setsize(inode, attr->ia_size);
276 		if (error)
277 			return error;
278 	}
279 
280 	setattr_copy(inode, attr);
281 	mark_inode_dirty(inode);
282 	return 0;
283 }
284 
285 const struct inode_operations udf_file_inode_operations = {
286 	.setattr		= udf_setattr,
287 };
288