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