Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Inode operations for Coda filesystem |
| 4 | * Original version: (C) 1996 P. Braam and M. Callahan |
| 5 | * Rewritten for Linux 2.1. (C) 1997 Carnegie Mellon University |
| 6 | * |
| 7 | * Carnegie Mellon encourages users to contribute improvements to |
| 8 | * the Coda project. Contact Peter Braam (coda@cs.cmu.edu). |
| 9 | */ |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/time.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/stat.h> |
| 16 | #include <linux/errno.h> |
Fabian Frederick | 834b46c | 2014-08-08 14:20:33 -0700 | [diff] [blame] | 17 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/string.h> |
| 19 | |
| 20 | #include <linux/coda.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/coda_psdev.h> |
Al Viro | 31a203d | 2011-01-12 16:36:09 -0500 | [diff] [blame] | 22 | #include "coda_linux.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | /* initialize the debugging variables */ |
| 25 | int coda_fake_statfs; |
| 26 | |
| 27 | /* print a fid */ |
| 28 | char * coda_f2s(struct CodaFid *f) |
| 29 | { |
| 30 | static char s[60]; |
Adrian Bunk | de0ca06 | 2008-07-25 01:46:34 -0700 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | sprintf(s, "(%08x.%08x.%08x.%08x)", f->opaque[0], f->opaque[1], f->opaque[2], f->opaque[3]); |
Adrian Bunk | de0ca06 | 2008-07-25 01:46:34 -0700 | [diff] [blame] | 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | return s; |
| 35 | } |
| 36 | |
| 37 | /* recognize special .CONTROL name */ |
| 38 | int coda_iscontrol(const char *name, size_t length) |
| 39 | { |
| 40 | return ((CODA_CONTROLLEN == length) && |
| 41 | (strncmp(name, CODA_CONTROL, CODA_CONTROLLEN) == 0)); |
| 42 | } |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | unsigned short coda_flags_to_cflags(unsigned short flags) |
| 45 | { |
| 46 | unsigned short coda_flags = 0; |
| 47 | |
| 48 | if ((flags & O_ACCMODE) == O_RDONLY) |
| 49 | coda_flags |= C_O_READ; |
| 50 | |
| 51 | if ((flags & O_ACCMODE) == O_RDWR) |
| 52 | coda_flags |= C_O_READ | C_O_WRITE; |
| 53 | |
| 54 | if ((flags & O_ACCMODE) == O_WRONLY) |
| 55 | coda_flags |= C_O_WRITE; |
| 56 | |
| 57 | if (flags & O_TRUNC) |
| 58 | coda_flags |= C_O_TRUNC; |
| 59 | |
| 60 | if (flags & O_CREAT) |
| 61 | coda_flags |= C_O_CREAT; |
| 62 | |
| 63 | if (flags & O_EXCL) |
| 64 | coda_flags |= C_O_EXCL; |
| 65 | |
| 66 | return coda_flags; |
| 67 | } |
| 68 | |
Arnd Bergmann | 6ced9aa | 2019-07-16 16:28:32 -0700 | [diff] [blame^] | 69 | static struct timespec64 coda_to_timespec64(struct vtimespec ts) |
| 70 | { |
| 71 | /* |
| 72 | * We interpret incoming timestamps as 'signed' to match traditional |
| 73 | * usage and support pre-1970 timestamps, but this breaks in y2038 |
| 74 | * on 32-bit machines. |
| 75 | */ |
| 76 | struct timespec64 ts64 = { |
| 77 | .tv_sec = ts.tv_sec, |
| 78 | .tv_nsec = ts.tv_nsec, |
| 79 | }; |
| 80 | |
| 81 | return ts64; |
| 82 | } |
| 83 | |
| 84 | static struct vtimespec timespec64_to_coda(struct timespec64 ts64) |
| 85 | { |
| 86 | /* clamp the timestamps to the maximum range rather than wrapping */ |
| 87 | struct vtimespec ts = { |
| 88 | .tv_sec = lower_32_bits(clamp_t(time64_t, ts64.tv_sec, |
| 89 | LONG_MIN, LONG_MAX)), |
| 90 | .tv_nsec = ts64.tv_nsec, |
| 91 | }; |
| 92 | |
| 93 | return ts; |
| 94 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* utility functions below */ |
| 97 | void coda_vattr_to_iattr(struct inode *inode, struct coda_vattr *attr) |
| 98 | { |
| 99 | int inode_type; |
| 100 | /* inode's i_flags, i_ino are set by iget |
| 101 | XXX: is this all we need ?? |
| 102 | */ |
| 103 | switch (attr->va_type) { |
| 104 | case C_VNON: |
| 105 | inode_type = 0; |
| 106 | break; |
| 107 | case C_VREG: |
| 108 | inode_type = S_IFREG; |
| 109 | break; |
| 110 | case C_VDIR: |
| 111 | inode_type = S_IFDIR; |
| 112 | break; |
| 113 | case C_VLNK: |
| 114 | inode_type = S_IFLNK; |
| 115 | break; |
| 116 | default: |
| 117 | inode_type = 0; |
| 118 | } |
| 119 | inode->i_mode |= inode_type; |
| 120 | |
| 121 | if (attr->va_mode != (u_short) -1) |
| 122 | inode->i_mode = attr->va_mode | inode_type; |
| 123 | if (attr->va_uid != -1) |
Eric W. Biederman | d83f590 | 2013-01-30 19:21:14 -0800 | [diff] [blame] | 124 | inode->i_uid = make_kuid(&init_user_ns, (uid_t) attr->va_uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | if (attr->va_gid != -1) |
Eric W. Biederman | d83f590 | 2013-01-30 19:21:14 -0800 | [diff] [blame] | 126 | inode->i_gid = make_kgid(&init_user_ns, (gid_t) attr->va_gid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | if (attr->va_nlink != -1) |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 128 | set_nlink(inode, attr->va_nlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | if (attr->va_size != -1) |
| 130 | inode->i_size = attr->va_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (attr->va_size != -1) |
| 132 | inode->i_blocks = (attr->va_size + 511) >> 9; |
| 133 | if (attr->va_atime.tv_sec != -1) |
Arnd Bergmann | 6ced9aa | 2019-07-16 16:28:32 -0700 | [diff] [blame^] | 134 | inode->i_atime = coda_to_timespec64(attr->va_atime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | if (attr->va_mtime.tv_sec != -1) |
Arnd Bergmann | 6ced9aa | 2019-07-16 16:28:32 -0700 | [diff] [blame^] | 136 | inode->i_mtime = coda_to_timespec64(attr->va_mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | if (attr->va_ctime.tv_sec != -1) |
Arnd Bergmann | 6ced9aa | 2019-07-16 16:28:32 -0700 | [diff] [blame^] | 138 | inode->i_ctime = coda_to_timespec64(attr->va_ctime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
| 142 | /* |
| 143 | * BSD sets attributes that need not be modified to -1. |
| 144 | * Linux uses the valid field to indicate what should be |
| 145 | * looked at. The BSD type field needs to be deduced from linux |
| 146 | * mode. |
| 147 | * So we have to do some translations here. |
| 148 | */ |
| 149 | |
| 150 | void coda_iattr_to_vattr(struct iattr *iattr, struct coda_vattr *vattr) |
| 151 | { |
| 152 | unsigned int valid; |
| 153 | |
| 154 | /* clean out */ |
Andrew Morton | a2d416d | 2008-04-29 00:59:22 -0700 | [diff] [blame] | 155 | vattr->va_mode = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | vattr->va_uid = (vuid_t) -1; |
| 157 | vattr->va_gid = (vgid_t) -1; |
| 158 | vattr->va_size = (off_t) -1; |
Arnd Bergmann | 6ced9aa | 2019-07-16 16:28:32 -0700 | [diff] [blame^] | 159 | vattr->va_atime.tv_sec = (long) -1; |
| 160 | vattr->va_atime.tv_nsec = (long) -1; |
| 161 | vattr->va_mtime.tv_sec = (long) -1; |
| 162 | vattr->va_mtime.tv_nsec = (long) -1; |
| 163 | vattr->va_ctime.tv_sec = (long) -1; |
| 164 | vattr->va_ctime.tv_nsec = (long) -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | vattr->va_type = C_VNON; |
| 166 | vattr->va_fileid = -1; |
| 167 | vattr->va_gen = -1; |
| 168 | vattr->va_bytes = -1; |
| 169 | vattr->va_nlink = -1; |
| 170 | vattr->va_blocksize = -1; |
| 171 | vattr->va_rdev = -1; |
| 172 | vattr->va_flags = 0; |
| 173 | |
| 174 | /* determine the type */ |
| 175 | #if 0 |
| 176 | mode = iattr->ia_mode; |
| 177 | if ( S_ISDIR(mode) ) { |
| 178 | vattr->va_type = C_VDIR; |
| 179 | } else if ( S_ISREG(mode) ) { |
| 180 | vattr->va_type = C_VREG; |
| 181 | } else if ( S_ISLNK(mode) ) { |
| 182 | vattr->va_type = C_VLNK; |
| 183 | } else { |
| 184 | /* don't do others */ |
| 185 | vattr->va_type = C_VNON; |
| 186 | } |
| 187 | #endif |
| 188 | |
| 189 | /* set those vattrs that need change */ |
| 190 | valid = iattr->ia_valid; |
| 191 | if ( valid & ATTR_MODE ) { |
| 192 | vattr->va_mode = iattr->ia_mode; |
| 193 | } |
| 194 | if ( valid & ATTR_UID ) { |
Eric W. Biederman | d83f590 | 2013-01-30 19:21:14 -0800 | [diff] [blame] | 195 | vattr->va_uid = (vuid_t) from_kuid(&init_user_ns, iattr->ia_uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | if ( valid & ATTR_GID ) { |
Eric W. Biederman | d83f590 | 2013-01-30 19:21:14 -0800 | [diff] [blame] | 198 | vattr->va_gid = (vgid_t) from_kgid(&init_user_ns, iattr->ia_gid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | if ( valid & ATTR_SIZE ) { |
| 201 | vattr->va_size = iattr->ia_size; |
| 202 | } |
| 203 | if ( valid & ATTR_ATIME ) { |
Arnd Bergmann | 6ced9aa | 2019-07-16 16:28:32 -0700 | [diff] [blame^] | 204 | vattr->va_atime = timespec64_to_coda(iattr->ia_atime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | if ( valid & ATTR_MTIME ) { |
Arnd Bergmann | 6ced9aa | 2019-07-16 16:28:32 -0700 | [diff] [blame^] | 207 | vattr->va_mtime = timespec64_to_coda(iattr->ia_mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | if ( valid & ATTR_CTIME ) { |
Arnd Bergmann | 6ced9aa | 2019-07-16 16:28:32 -0700 | [diff] [blame^] | 210 | vattr->va_ctime = timespec64_to_coda(iattr->ia_ctime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |