1 /*
2  * file.c
3  *
4  * PURPOSE
5  *  File handling routines for the OSTA-UDF(tm) filesystem.
6  *
7  * CONTACTS
8  *  E-mail regarding any portion of the Linux UDF file system should be
9  *  directed to the development team mailing list (run by majordomo):
10  *    linux_udf@hpesjro.fc.hp.com
11  *
12  * COPYRIGHT
13  *  This file is distributed under the terms of the GNU General Public
14  *  License (GPL). Copies of the GPL can be obtained from:
15  *    ftp://prep.ai.mit.edu/pub/gnu/GPL
16  *  Each contributing author retains all rights to their own work.
17  *
18  *  (C) 1998-1999 Dave Boynton
19  *  (C) 1998-2001 Ben Fennema
20  *  (C) 1999-2000 Stelias Computing Inc
21  *
22  * HISTORY
23  *
24  *  10/02/98 dgb  Attempt to integrate into udf.o
25  *  10/07/98      Switched to using generic_readpage, etc., like isofs
26  *                And it works!
27  *  12/06/98 blf  Added udf_file_read. uses generic_file_read for all cases but
28  *                ICBTAG_FLAG_AD_IN_ICB.
29  *  04/06/99      64 bit file handling on 32 bit systems taken from ext2 file.c
30  *  05/12/99      Preliminary file write support
31  */
32 
33 #include "udfdecl.h"
34 #include <linux/fs.h>
35 #include <linux/udf_fs.h>
36 #include <asm/uaccess.h>
37 #include <linux/kernel.h>
38 #include <linux/string.h> /* memset */
39 #include <linux/errno.h>
40 #include <linux/locks.h>
41 #include <linux/smp_lock.h>
42 
43 #include "udf_i.h"
44 #include "udf_sb.h"
45 
udf_adinicb_readpage(struct file * file,struct page * page)46 static int udf_adinicb_readpage(struct file *file, struct page * page)
47 {
48 	struct inode *inode = page->mapping->host;
49 
50 	struct buffer_head *bh;
51 	int block;
52 	char *kaddr;
53 	int err = 0;
54 
55 	if (!PageLocked(page))
56 		PAGE_BUG(page);
57 
58 	kaddr = kmap(page);
59 	memset(kaddr, 0, PAGE_CACHE_SIZE);
60 	block = udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0);
61 	bh = sb_bread(inode->i_sb, block);
62 	if (!bh)
63 	{
64 		SetPageError(page);
65 		err = -EIO;
66 		goto out;
67 	}
68 	memcpy(kaddr, bh->b_data + udf_ext0_offset(inode), inode->i_size);
69 	brelse(bh);
70 	flush_dcache_page(page);
71 	SetPageUptodate(page);
72 out:
73 	kunmap(page);
74 	UnlockPage(page);
75 	return err;
76 }
77 
udf_adinicb_writepage(struct page * page)78 static int udf_adinicb_writepage(struct page *page)
79 {
80 	struct inode *inode = page->mapping->host;
81 
82 	struct buffer_head *bh;
83 	int block;
84 	char *kaddr;
85 	int err = 0;
86 
87 	if (!PageLocked(page))
88 		PAGE_BUG(page);
89 
90 	kaddr = kmap(page);
91 	block = udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0);
92 	bh = sb_bread(inode->i_sb, block);
93 	if (!bh)
94 	{
95 		SetPageError(page);
96 		err = -EIO;
97 		goto out;
98 	}
99 	memcpy(bh->b_data + udf_ext0_offset(inode), kaddr, inode->i_size);
100 	mark_buffer_dirty(bh);
101 	brelse(bh);
102 	SetPageUptodate(page);
103 out:
104 	kunmap(page);
105 	UnlockPage(page);
106 	return err;
107 }
108 
udf_adinicb_prepare_write(struct file * file,struct page * page,unsigned offset,unsigned to)109 static int udf_adinicb_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
110 {
111 	kmap(page);
112 	return 0;
113 }
114 
udf_adinicb_commit_write(struct file * file,struct page * page,unsigned offset,unsigned to)115 static int udf_adinicb_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
116 {
117 	struct inode *inode = page->mapping->host;
118 
119 	struct buffer_head *bh;
120 	int block;
121 	char *kaddr = page_address(page);
122 	int err = 0;
123 
124 	block = udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0);
125 	bh = sb_bread(inode->i_sb, block);
126 	if (!bh)
127 	{
128 		SetPageError(page);
129 		err = -EIO;
130 		goto out;
131 	}
132 	memcpy(bh->b_data + udf_file_entry_alloc_offset(inode) + offset,
133 		kaddr + offset, to - offset);
134 	mark_buffer_dirty(bh);
135 	brelse(bh);
136 	SetPageUptodate(page);
137 out:
138 	kunmap(page);
139 	/* only one page here */
140 	if (to > inode->i_size)
141 		inode->i_size = to;
142 	return err;
143 }
144 
145 struct address_space_operations udf_adinicb_aops = {
146 	readpage:		udf_adinicb_readpage,
147 	writepage:		udf_adinicb_writepage,
148 	sync_page:		block_sync_page,
149 	prepare_write:		udf_adinicb_prepare_write,
150 	commit_write:		udf_adinicb_commit_write,
151 };
152 
udf_file_write(struct file * file,const char * buf,size_t count,loff_t * ppos)153 static ssize_t udf_file_write(struct file * file, const char * buf,
154 	size_t count, loff_t *ppos)
155 {
156 	ssize_t retval;
157 	struct inode *inode = file->f_dentry->d_inode;
158 	int err;
159 	loff_t pos;
160 
161 	if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
162 	{
163 		if (file->f_flags & O_APPEND)
164 			pos = inode->i_size;
165 		else
166 			pos = *ppos;
167 
168 		if (pos < 0 || pos + count < pos)
169 			return 0;
170 
171 		if (inode->i_sb->s_blocksize - udf_file_entry_alloc_offset(inode) <
172 			pos + count)
173 		{
174 			udf_expand_file_adinicb(inode, pos + count, &err);
175 			if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
176 			{
177 				udf_debug("udf_expand_adinicb: err=%d\n", err);
178 				return err;
179 			}
180 		}
181 		else
182 		{
183 			if (pos + count > inode->i_size)
184 				UDF_I_LENALLOC(inode) = pos + count;
185 			else
186 				UDF_I_LENALLOC(inode) = inode->i_size;
187 		}
188 	}
189 
190 	retval = generic_file_write(file, buf, count, ppos);
191 
192 	if (retval > 0)
193 	{
194 		UDF_I_UCTIME(inode) = UDF_I_UMTIME(inode) = CURRENT_UTIME;
195 		mark_inode_dirty(inode);
196 	}
197 	return retval;
198 }
199 
200 /*
201  * udf_ioctl
202  *
203  * PURPOSE
204  *	Issue an ioctl.
205  *
206  * DESCRIPTION
207  *	Optional - sys_ioctl() will return -ENOTTY if this routine is not
208  *	available, and the ioctl cannot be handled without filesystem help.
209  *
210  *	sys_ioctl() handles these ioctls that apply only to regular files:
211  *		FIBMAP [requires udf_block_map()], FIGETBSZ, FIONREAD
212  *	These ioctls are also handled by sys_ioctl():
213  *		FIOCLEX, FIONCLEX, FIONBIO, FIOASYNC
214  *	All other ioctls are passed to the filesystem.
215  *
216  *	Refer to sys_ioctl() in fs/ioctl.c
217  *	sys_ioctl() -> .
218  *
219  * PRE-CONDITIONS
220  *	inode			Pointer to inode that ioctl was issued on.
221  *	filp			Pointer to file that ioctl was issued on.
222  *	cmd			The ioctl command.
223  *	arg			The ioctl argument [can be interpreted as a
224  *				user-space pointer if desired].
225  *
226  * POST-CONDITIONS
227  *	<return>		Success (>=0) or an error code (<=0) that
228  *				sys_ioctl() will return.
229  *
230  * HISTORY
231  *	July 1, 1997 - Andrew E. Mileski
232  *	Written, tested, and released.
233  */
udf_ioctl(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)234 int udf_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
235 	unsigned long arg)
236 {
237 	int result = -EINVAL;
238 	struct buffer_head *bh = NULL;
239 	long_ad eaicb;
240 	uint8_t *ea = NULL;
241 
242 	if ( permission(inode, MAY_READ) != 0 )
243 	{
244 		udf_debug("no permission to access inode %lu\n",
245 						inode->i_ino);
246 		return -EPERM;
247 	}
248 
249 	if ( !arg )
250 	{
251 		udf_debug("invalid argument to udf_ioctl\n");
252 		return -EINVAL;
253 	}
254 
255 	/* first, do ioctls that don't need to udf_read */
256 	switch (cmd)
257 	{
258 		case UDF_GETVOLIDENT:
259 			return copy_to_user((char *)arg,
260 				UDF_SB_VOLIDENT(inode->i_sb), 32) ? -EFAULT : 0;
261 		case UDF_RELOCATE_BLOCKS:
262 		{
263 			long old, new;
264 
265 			if (!capable(CAP_SYS_ADMIN)) return -EACCES;
266 			if (get_user(old, (long *)arg)) return -EFAULT;
267 			if ((result = udf_relocate_blocks(inode->i_sb,
268 					old, &new)) == 0)
269 				result = put_user(new, (long *)arg);
270 
271 			return result;
272 		}
273 	}
274 
275 	/* ok, we need to read the inode */
276 	bh = udf_tread(inode->i_sb,
277 		udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0));
278 
279 	if (!bh)
280 	{
281 		udf_debug("bread failed (inode=%ld)\n", inode->i_ino);
282 		return -EIO;
283 	}
284 
285 	if (UDF_I_EXTENDED_FE(inode) == 0)
286 	{
287 		struct fileEntry *fe;
288 
289 		fe = (struct fileEntry *)bh->b_data;
290 		eaicb = lela_to_cpu(fe->extendedAttrICB);
291 		if (UDF_I_LENEATTR(inode))
292 			ea = fe->extendedAttr;
293 	}
294 	else
295 	{
296 		struct extendedFileEntry *efe;
297 
298 		efe = (struct extendedFileEntry *)bh->b_data;
299 		eaicb = lela_to_cpu(efe->extendedAttrICB);
300 		if (UDF_I_LENEATTR(inode))
301 			ea = efe->extendedAttr;
302 	}
303 
304 	switch (cmd)
305 	{
306 		case UDF_GETEASIZE:
307 			result = put_user(UDF_I_LENEATTR(inode), (int *)arg);
308 			break;
309 
310 		case UDF_GETEABLOCK:
311 			result = copy_to_user((char *)arg, ea,
312 				UDF_I_LENEATTR(inode)) ? -EFAULT : 0;
313 			break;
314 	}
315 
316 	udf_release_data(bh);
317 	return result;
318 }
319 
320 /*
321  * udf_release_file
322  *
323  * PURPOSE
324  *  Called when all references to the file are closed
325  *
326  * DESCRIPTION
327  *  Discard prealloced blocks
328  *
329  * HISTORY
330  *
331  */
udf_release_file(struct inode * inode,struct file * filp)332 static int udf_release_file(struct inode * inode, struct file * filp)
333 {
334 	if (filp->f_mode & FMODE_WRITE)
335 	{
336 		lock_kernel();
337 		udf_discard_prealloc(inode);
338 		unlock_kernel();
339 	}
340 	return 0;
341 }
342 
343 /*
344  * udf_open_file
345  *
346  * PURPOSE
347  *  Called when an inode is about to be open.
348  *
349  * DESCRIPTION
350  *  Use this to disallow opening RW large files on 32 bit systems.
351  *  On 64 bit systems we force on O_LARGEFILE in sys_open.
352  *
353  * HISTORY
354  *
355  */
udf_open_file(struct inode * inode,struct file * filp)356 static int udf_open_file(struct inode * inode, struct file * filp)
357 {
358 	if ((inode->i_size & 0xFFFFFFFF80000000ULL) && !(filp->f_flags & O_LARGEFILE))
359 		return -EFBIG;
360 	return 0;
361 }
362 
363 struct file_operations udf_file_operations = {
364 	read:			generic_file_read,
365 	ioctl:			udf_ioctl,
366 	open:			udf_open_file,
367 	mmap:			generic_file_mmap,
368 	write:			udf_file_write,
369 	release:		udf_release_file,
370 	fsync:			udf_fsync_file,
371 };
372 
373 struct inode_operations udf_file_inode_operations = {
374 	truncate:		udf_truncate,
375 };
376