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