blob: 903ca8fa4b9b87a74c2b73b0a11fe3b221508366 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Frederick834b46c2014-08-08 14:20:33 -070017#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/string.h>
19
20#include <linux/coda.h>
David Howells8fc8b9d2019-07-16 16:28:47 -070021#include "coda_psdev.h"
Al Viro31a203d2011-01-12 16:36:09 -050022#include "coda_linux.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/* initialize the debugging variables */
25int coda_fake_statfs;
26
27/* print a fid */
28char * coda_f2s(struct CodaFid *f)
29{
30 static char s[60];
Adrian Bunkde0ca062008-07-25 01:46:34 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 sprintf(s, "(%08x.%08x.%08x.%08x)", f->opaque[0], f->opaque[1], f->opaque[2], f->opaque[3]);
Adrian Bunkde0ca062008-07-25 01:46:34 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return s;
35}
36
37/* recognize special .CONTROL name */
38int 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 Torvalds1da177e2005-04-16 15:20:36 -070044unsigned 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
Jan Harkes5e7c31d2019-07-16 16:28:35 -070069static struct timespec64 coda_to_timespec64(struct coda_timespec ts)
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -070070{
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -070071 struct timespec64 ts64 = {
72 .tv_sec = ts.tv_sec,
73 .tv_nsec = ts.tv_nsec,
74 };
75
76 return ts64;
77}
78
Jan Harkes5e7c31d2019-07-16 16:28:35 -070079static struct coda_timespec timespec64_to_coda(struct timespec64 ts64)
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -070080{
Jan Harkes5e7c31d2019-07-16 16:28:35 -070081 struct coda_timespec ts = {
82 .tv_sec = ts64.tv_sec,
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -070083 .tv_nsec = ts64.tv_nsec,
84 };
85
86 return ts;
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/* utility functions below */
Jan Harkes5a646fb2021-11-08 18:34:45 -080090umode_t coda_inode_type(struct coda_vattr *attr)
91{
92 switch (attr->va_type) {
93 case C_VREG:
94 return S_IFREG;
95 case C_VDIR:
96 return S_IFDIR;
97 case C_VLNK:
98 return S_IFLNK;
99 case C_VNON:
100 default:
101 return 0;
102 }
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105void coda_vattr_to_iattr(struct inode *inode, struct coda_vattr *attr)
106{
Jan Harkes5a646fb2021-11-08 18:34:45 -0800107 /* inode's i_flags, i_ino are set by iget
108 * XXX: is this all we need ??
109 */
110 umode_t inode_type = coda_inode_type(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 inode->i_mode |= inode_type;
112
113 if (attr->va_mode != (u_short) -1)
114 inode->i_mode = attr->va_mode | inode_type;
115 if (attr->va_uid != -1)
Eric W. Biedermand83f5902013-01-30 19:21:14 -0800116 inode->i_uid = make_kuid(&init_user_ns, (uid_t) attr->va_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (attr->va_gid != -1)
Eric W. Biedermand83f5902013-01-30 19:21:14 -0800118 inode->i_gid = make_kgid(&init_user_ns, (gid_t) attr->va_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (attr->va_nlink != -1)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200120 set_nlink(inode, attr->va_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (attr->va_size != -1)
122 inode->i_size = attr->va_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (attr->va_size != -1)
124 inode->i_blocks = (attr->va_size + 511) >> 9;
125 if (attr->va_atime.tv_sec != -1)
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -0700126 inode->i_atime = coda_to_timespec64(attr->va_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (attr->va_mtime.tv_sec != -1)
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -0700128 inode->i_mtime = coda_to_timespec64(attr->va_mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (attr->va_ctime.tv_sec != -1)
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -0700130 inode->i_ctime = coda_to_timespec64(attr->va_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133
134/*
135 * BSD sets attributes that need not be modified to -1.
136 * Linux uses the valid field to indicate what should be
137 * looked at. The BSD type field needs to be deduced from linux
138 * mode.
139 * So we have to do some translations here.
140 */
141
142void coda_iattr_to_vattr(struct iattr *iattr, struct coda_vattr *vattr)
143{
144 unsigned int valid;
145
146 /* clean out */
Andrew Mortona2d416d2008-04-29 00:59:22 -0700147 vattr->va_mode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 vattr->va_uid = (vuid_t) -1;
149 vattr->va_gid = (vgid_t) -1;
150 vattr->va_size = (off_t) -1;
Jan Harkes5e7c31d2019-07-16 16:28:35 -0700151 vattr->va_atime.tv_sec = (int64_t) -1;
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -0700152 vattr->va_atime.tv_nsec = (long) -1;
Jan Harkes5e7c31d2019-07-16 16:28:35 -0700153 vattr->va_mtime.tv_sec = (int64_t) -1;
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -0700154 vattr->va_mtime.tv_nsec = (long) -1;
Jan Harkes5e7c31d2019-07-16 16:28:35 -0700155 vattr->va_ctime.tv_sec = (int64_t) -1;
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -0700156 vattr->va_ctime.tv_nsec = (long) -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 vattr->va_type = C_VNON;
158 vattr->va_fileid = -1;
159 vattr->va_gen = -1;
160 vattr->va_bytes = -1;
161 vattr->va_nlink = -1;
162 vattr->va_blocksize = -1;
163 vattr->va_rdev = -1;
164 vattr->va_flags = 0;
165
166 /* determine the type */
167#if 0
168 mode = iattr->ia_mode;
169 if ( S_ISDIR(mode) ) {
170 vattr->va_type = C_VDIR;
171 } else if ( S_ISREG(mode) ) {
172 vattr->va_type = C_VREG;
173 } else if ( S_ISLNK(mode) ) {
174 vattr->va_type = C_VLNK;
175 } else {
176 /* don't do others */
177 vattr->va_type = C_VNON;
178 }
179#endif
180
181 /* set those vattrs that need change */
182 valid = iattr->ia_valid;
183 if ( valid & ATTR_MODE ) {
184 vattr->va_mode = iattr->ia_mode;
185 }
186 if ( valid & ATTR_UID ) {
Eric W. Biedermand83f5902013-01-30 19:21:14 -0800187 vattr->va_uid = (vuid_t) from_kuid(&init_user_ns, iattr->ia_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 if ( valid & ATTR_GID ) {
Eric W. Biedermand83f5902013-01-30 19:21:14 -0800190 vattr->va_gid = (vgid_t) from_kgid(&init_user_ns, iattr->ia_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192 if ( valid & ATTR_SIZE ) {
193 vattr->va_size = iattr->ia_size;
194 }
195 if ( valid & ATTR_ATIME ) {
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -0700196 vattr->va_atime = timespec64_to_coda(iattr->ia_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198 if ( valid & ATTR_MTIME ) {
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -0700199 vattr->va_mtime = timespec64_to_coda(iattr->ia_mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 if ( valid & ATTR_CTIME ) {
Arnd Bergmann6ced9aa2019-07-16 16:28:32 -0700202 vattr->va_ctime = timespec64_to_coda(iattr->ia_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204}
205