Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

new helpers: no_seek_end_llseek{,_size}()

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

Al Viro b25472f9 b808b1d6

+54 -222
+2 -18
arch/sparc/kernel/mdesc.c
··· 1033 1033 1034 1034 static loff_t mdesc_llseek(struct file *file, loff_t offset, int whence) 1035 1035 { 1036 - struct mdesc_handle *hp; 1036 + struct mdesc_handle *hp = file->private_data; 1037 1037 1038 - switch (whence) { 1039 - case SEEK_CUR: 1040 - offset += file->f_pos; 1041 - break; 1042 - case SEEK_SET: 1043 - break; 1044 - default: 1045 - return -EINVAL; 1046 - } 1047 - 1048 - hp = file->private_data; 1049 - if (offset > hp->handle_size) 1050 - return -EINVAL; 1051 - else 1052 - file->f_pos = offset; 1053 - 1054 - return offset; 1038 + return no_seek_end_llseek_size(file, offset, whence, hp->handle_size); 1055 1039 } 1056 1040 1057 1041 /* mdesc_close() - /dev/mdesc is being closed, release the reference to
+1 -23
arch/x86/kernel/cpuid.c
··· 58 58 &cmd->eax, &cmd->ebx, &cmd->ecx, &cmd->edx); 59 59 } 60 60 61 - static loff_t cpuid_seek(struct file *file, loff_t offset, int orig) 62 - { 63 - loff_t ret; 64 - struct inode *inode = file->f_mapping->host; 65 - 66 - mutex_lock(&inode->i_mutex); 67 - switch (orig) { 68 - case 0: 69 - file->f_pos = offset; 70 - ret = file->f_pos; 71 - break; 72 - case 1: 73 - file->f_pos += offset; 74 - ret = file->f_pos; 75 - break; 76 - default: 77 - ret = -EINVAL; 78 - } 79 - mutex_unlock(&inode->i_mutex); 80 - return ret; 81 - } 82 - 83 61 static ssize_t cpuid_read(struct file *file, char __user *buf, 84 62 size_t count, loff_t *ppos) 85 63 { ··· 110 132 */ 111 133 static const struct file_operations cpuid_fops = { 112 134 .owner = THIS_MODULE, 113 - .llseek = cpuid_seek, 135 + .llseek = no_seek_end_llseek, 114 136 .read = cpuid_read, 115 137 .open = cpuid_open, 116 138 };
+1 -23
arch/x86/kernel/msr.c
··· 45 45 46 46 static struct class *msr_class; 47 47 48 - static loff_t msr_seek(struct file *file, loff_t offset, int orig) 49 - { 50 - loff_t ret; 51 - struct inode *inode = file_inode(file); 52 - 53 - mutex_lock(&inode->i_mutex); 54 - switch (orig) { 55 - case SEEK_SET: 56 - file->f_pos = offset; 57 - ret = file->f_pos; 58 - break; 59 - case SEEK_CUR: 60 - file->f_pos += offset; 61 - ret = file->f_pos; 62 - break; 63 - default: 64 - ret = -EINVAL; 65 - } 66 - mutex_unlock(&inode->i_mutex); 67 - return ret; 68 - } 69 - 70 48 static ssize_t msr_read(struct file *file, char __user *buf, 71 49 size_t count, loff_t *ppos) 72 50 { ··· 172 194 */ 173 195 static const struct file_operations msr_fops = { 174 196 .owner = THIS_MODULE, 175 - .llseek = msr_seek, 197 + .llseek = no_seek_end_llseek, 176 198 .read = msr_read, 177 199 .write = msr_write, 178 200 .open = msr_open,
+1 -30
drivers/char/nwflash.c
··· 277 277 printk(KERN_DEBUG "flash_llseek: offset=0x%X, orig=0x%X.\n", 278 278 (unsigned int) offset, orig); 279 279 280 - switch (orig) { 281 - case 0: 282 - if (offset < 0) { 283 - ret = -EINVAL; 284 - break; 285 - } 286 - 287 - if ((unsigned int) offset > gbFlashSize) { 288 - ret = -EINVAL; 289 - break; 290 - } 291 - 292 - file->f_pos = (unsigned int) offset; 293 - ret = file->f_pos; 294 - break; 295 - case 1: 296 - if ((file->f_pos + offset) > gbFlashSize) { 297 - ret = -EINVAL; 298 - break; 299 - } 300 - if ((file->f_pos + offset) < 0) { 301 - ret = -EINVAL; 302 - break; 303 - } 304 - file->f_pos += offset; 305 - ret = file->f_pos; 306 - break; 307 - default: 308 - ret = -EINVAL; 309 - } 280 + ret = no_seek_end_llseek_size(file, offset, orig, gbFlashSize); 310 281 mutex_unlock(&flash_mutex); 311 282 return ret; 312 283 }
+1 -16
drivers/net/wireless/ti/wlcore/debugfs.c
··· 1205 1205 1206 1206 static loff_t dev_mem_seek(struct file *file, loff_t offset, int orig) 1207 1207 { 1208 - loff_t ret; 1209 - 1210 1208 /* only requests of dword-aligned size and offset are supported */ 1211 1209 if (offset % 4) 1212 1210 return -EINVAL; 1213 1211 1214 - switch (orig) { 1215 - case SEEK_SET: 1216 - file->f_pos = offset; 1217 - ret = file->f_pos; 1218 - break; 1219 - case SEEK_CUR: 1220 - file->f_pos += offset; 1221 - ret = file->f_pos; 1222 - break; 1223 - default: 1224 - ret = -EINVAL; 1225 - } 1226 - 1227 - return ret; 1212 + return no_seek_end_llseek(file, offset, orig); 1228 1213 } 1229 1214 1230 1215 static const struct file_operations dev_mem_ops = {
+1 -14
drivers/s390/char/vmur.c
··· 782 782 783 783 static loff_t ur_llseek(struct file *file, loff_t offset, int whence) 784 784 { 785 - loff_t newpos; 786 - 787 785 if ((file->f_flags & O_ACCMODE) != O_RDONLY) 788 786 return -ESPIPE; /* seek allowed only for reader */ 789 787 if (offset % PAGE_SIZE) 790 788 return -ESPIPE; /* only multiples of 4K allowed */ 791 - switch (whence) { 792 - case 0: /* SEEK_SET */ 793 - newpos = offset; 794 - break; 795 - case 1: /* SEEK_CUR */ 796 - newpos = file->f_pos + offset; 797 - break; 798 - default: 799 - return -EINVAL; 800 - } 801 - file->f_pos = newpos; 802 - return newpos; 789 + return no_seek_end_llseek(file, offset, whence); 803 790 } 804 791 805 792 static const struct file_operations ur_fops = {
+1 -12
drivers/s390/char/zcore.c
··· 385 385 loff_t rc; 386 386 387 387 mutex_lock(&zcore_mutex); 388 - switch (orig) { 389 - case 0: 390 - file->f_pos = offset; 391 - rc = file->f_pos; 392 - break; 393 - case 1: 394 - file->f_pos += offset; 395 - rc = file->f_pos; 396 - break; 397 - default: 398 - rc = -EINVAL; 399 - } 388 + rc = no_seek_end_llseek(file, offset, orig); 400 389 mutex_unlock(&zcore_mutex); 401 390 return rc; 402 391 }
+1 -25
drivers/usb/core/devices.c
··· 661 661 return 0; 662 662 } 663 663 664 - static loff_t usb_device_lseek(struct file *file, loff_t offset, int orig) 665 - { 666 - loff_t ret; 667 - 668 - mutex_lock(&file_inode(file)->i_mutex); 669 - 670 - switch (orig) { 671 - case 0: 672 - file->f_pos = offset; 673 - ret = file->f_pos; 674 - break; 675 - case 1: 676 - file->f_pos += offset; 677 - ret = file->f_pos; 678 - break; 679 - case 2: 680 - default: 681 - ret = -EINVAL; 682 - } 683 - 684 - mutex_unlock(&file_inode(file)->i_mutex); 685 - return ret; 686 - } 687 - 688 664 const struct file_operations usbfs_devices_fops = { 689 - .llseek = usb_device_lseek, 665 + .llseek = no_seek_end_llseek, 690 666 .read = usb_device_read, 691 667 .poll = usb_device_poll, 692 668 };
+1 -25
drivers/usb/core/devio.c
··· 157 157 ps->dev->state != USB_STATE_NOTATTACHED); 158 158 } 159 159 160 - static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig) 161 - { 162 - loff_t ret; 163 - 164 - mutex_lock(&file_inode(file)->i_mutex); 165 - 166 - switch (orig) { 167 - case 0: 168 - file->f_pos = offset; 169 - ret = file->f_pos; 170 - break; 171 - case 1: 172 - file->f_pos += offset; 173 - ret = file->f_pos; 174 - break; 175 - case 2: 176 - default: 177 - ret = -EINVAL; 178 - } 179 - 180 - mutex_unlock(&file_inode(file)->i_mutex); 181 - return ret; 182 - } 183 - 184 160 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, 185 161 loff_t *ppos) 186 162 { ··· 2342 2366 2343 2367 const struct file_operations usbdev_file_operations = { 2344 2368 .owner = THIS_MODULE, 2345 - .llseek = usbdev_lseek, 2369 + .llseek = no_seek_end_llseek, 2346 2370 .read = usbdev_read, 2347 2371 .poll = usbdev_poll, 2348 2372 .unlocked_ioctl = usbdev_ioctl,
+2 -21
drivers/usb/host/uhci-debug.c
··· 584 584 585 585 static loff_t uhci_debug_lseek(struct file *file, loff_t off, int whence) 586 586 { 587 - struct uhci_debug *up; 588 - loff_t new = -1; 589 - 590 - up = file->private_data; 591 - 592 - /* 593 - * XXX: atomic 64bit seek access, but that needs to be fixed in the VFS 594 - */ 595 - switch (whence) { 596 - case 0: 597 - new = off; 598 - break; 599 - case 1: 600 - new = file->f_pos + off; 601 - break; 602 - } 603 - 604 - if (new < 0 || new > up->size) 605 - return -EINVAL; 606 - 607 - return (file->f_pos = new); 587 + struct uhci_debug *up = file->private_data; 588 + return no_seek_end_llseek_size(file, off, whence, up->size); 608 589 } 609 590 610 591 static ssize_t uhci_debug_read(struct file *file, char __user *buf,
+1 -15
drivers/usb/misc/sisusbvga/sisusb.c
··· 2825 2825 return -ENODEV; 2826 2826 } 2827 2827 2828 - switch (orig) { 2829 - case 0: 2830 - file->f_pos = offset; 2831 - ret = file->f_pos; 2832 - /* never negative, no force_successful_syscall needed */ 2833 - break; 2834 - case 1: 2835 - file->f_pos += offset; 2836 - ret = file->f_pos; 2837 - /* never negative, no force_successful_syscall needed */ 2838 - break; 2839 - default: 2840 - /* seeking relative to "end of file" is not supported */ 2841 - ret = -EINVAL; 2842 - } 2828 + ret = no_seek_end_llseek(file, offset, orig); 2843 2829 2844 2830 mutex_unlock(&sisusb->lock); 2845 2831 return ret;
+39
fs/read_write.c
··· 171 171 EXPORT_SYMBOL(fixed_size_llseek); 172 172 173 173 /** 174 + * no_seek_end_llseek - llseek implementation for fixed-sized devices 175 + * @file: file structure to seek on 176 + * @offset: file offset to seek to 177 + * @whence: type of seek 178 + * 179 + */ 180 + loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence) 181 + { 182 + switch (whence) { 183 + case SEEK_SET: case SEEK_CUR: 184 + return generic_file_llseek_size(file, offset, whence, 185 + ~0ULL, 0); 186 + default: 187 + return -EINVAL; 188 + } 189 + } 190 + EXPORT_SYMBOL(no_seek_end_llseek); 191 + 192 + /** 193 + * no_seek_end_llseek_size - llseek implementation for fixed-sized devices 194 + * @file: file structure to seek on 195 + * @offset: file offset to seek to 196 + * @whence: type of seek 197 + * @size: maximal offset allowed 198 + * 199 + */ 200 + loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size) 201 + { 202 + switch (whence) { 203 + case SEEK_SET: case SEEK_CUR: 204 + return generic_file_llseek_size(file, offset, whence, 205 + size, 0); 206 + default: 207 + return -EINVAL; 208 + } 209 + } 210 + EXPORT_SYMBOL(no_seek_end_llseek_size); 211 + 212 + /** 174 213 * noop_llseek - No Operation Performed llseek implementation 175 214 * @file: file structure to seek on 176 215 * @offset: file offset to seek to
+2
include/linux/fs.h
··· 2660 2660 int whence, loff_t maxsize, loff_t eof); 2661 2661 extern loff_t fixed_size_llseek(struct file *file, loff_t offset, 2662 2662 int whence, loff_t size); 2663 + extern loff_t no_seek_end_llseek_size(struct file *, loff_t, int, loff_t); 2664 + extern loff_t no_seek_end_llseek(struct file *, loff_t, int); 2663 2665 extern int generic_file_open(struct inode * inode, struct file * filp); 2664 2666 extern int nonseekable_open(struct inode * inode, struct file * filp); 2665 2667