blob: f1714cfb589c6018adf3d37046ef53d117aab42e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Inode operations for Coda filesystem
3 * Original version: (C) 1996 P. Braam and M. Callahan
4 * Rewritten for Linux 2.1. (C) 1997 Carnegie Mellon University
5 *
6 * Carnegie Mellon encourages users to contribute improvements to
7 * the Coda project. Contact Peter Braam (coda@cs.cmu.edu).
8 */
9
10#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/time.h>
13#include <linux/fs.h>
14#include <linux/stat.h>
15#include <linux/errno.h>
Fabian Frederick834b46c2014-08-08 14:20:33 -070016#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/string.h>
18
19#include <linux/coda.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/coda_psdev.h>
Al Viro31a203d2011-01-12 16:36:09 -050021#include "coda_linux.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/* initialize the debugging variables */
24int coda_fake_statfs;
25
26/* print a fid */
27char * coda_f2s(struct CodaFid *f)
28{
29 static char s[60];
Adrian Bunkde0ca062008-07-25 01:46:34 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 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 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 return s;
34}
35
36/* recognize special .CONTROL name */
37int coda_iscontrol(const char *name, size_t length)
38{
39 return ((CODA_CONTROLLEN == length) &&
40 (strncmp(name, CODA_CONTROL, CODA_CONTROLLEN) == 0));
41}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043unsigned short coda_flags_to_cflags(unsigned short flags)
44{
45 unsigned short coda_flags = 0;
46
47 if ((flags & O_ACCMODE) == O_RDONLY)
48 coda_flags |= C_O_READ;
49
50 if ((flags & O_ACCMODE) == O_RDWR)
51 coda_flags |= C_O_READ | C_O_WRITE;
52
53 if ((flags & O_ACCMODE) == O_WRONLY)
54 coda_flags |= C_O_WRITE;
55
56 if (flags & O_TRUNC)
57 coda_flags |= C_O_TRUNC;
58
59 if (flags & O_CREAT)
60 coda_flags |= C_O_CREAT;
61
62 if (flags & O_EXCL)
63 coda_flags |= C_O_EXCL;
64
65 return coda_flags;
66}
67
68
69/* utility functions below */
70void coda_vattr_to_iattr(struct inode *inode, struct coda_vattr *attr)
71{
72 int inode_type;
73 /* inode's i_flags, i_ino are set by iget
74 XXX: is this all we need ??
75 */
76 switch (attr->va_type) {
77 case C_VNON:
78 inode_type = 0;
79 break;
80 case C_VREG:
81 inode_type = S_IFREG;
82 break;
83 case C_VDIR:
84 inode_type = S_IFDIR;
85 break;
86 case C_VLNK:
87 inode_type = S_IFLNK;
88 break;
89 default:
90 inode_type = 0;
91 }
92 inode->i_mode |= inode_type;
93
94 if (attr->va_mode != (u_short) -1)
95 inode->i_mode = attr->va_mode | inode_type;
96 if (attr->va_uid != -1)
Eric W. Biedermand83f5902013-01-30 19:21:14 -080097 inode->i_uid = make_kuid(&init_user_ns, (uid_t) attr->va_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (attr->va_gid != -1)
Eric W. Biedermand83f5902013-01-30 19:21:14 -080099 inode->i_gid = make_kgid(&init_user_ns, (gid_t) attr->va_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (attr->va_nlink != -1)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200101 set_nlink(inode, attr->va_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (attr->va_size != -1)
103 inode->i_size = attr->va_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (attr->va_size != -1)
105 inode->i_blocks = (attr->va_size + 511) >> 9;
106 if (attr->va_atime.tv_sec != -1)
107 inode->i_atime = attr->va_atime;
108 if (attr->va_mtime.tv_sec != -1)
109 inode->i_mtime = attr->va_mtime;
110 if (attr->va_ctime.tv_sec != -1)
111 inode->i_ctime = attr->va_ctime;
112}
113
114
115/*
116 * BSD sets attributes that need not be modified to -1.
117 * Linux uses the valid field to indicate what should be
118 * looked at. The BSD type field needs to be deduced from linux
119 * mode.
120 * So we have to do some translations here.
121 */
122
123void coda_iattr_to_vattr(struct iattr *iattr, struct coda_vattr *vattr)
124{
125 unsigned int valid;
126
127 /* clean out */
Andrew Mortona2d416d2008-04-29 00:59:22 -0700128 vattr->va_mode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 vattr->va_uid = (vuid_t) -1;
130 vattr->va_gid = (vgid_t) -1;
131 vattr->va_size = (off_t) -1;
132 vattr->va_atime.tv_sec = (time_t) -1;
133 vattr->va_atime.tv_nsec = (time_t) -1;
134 vattr->va_mtime.tv_sec = (time_t) -1;
135 vattr->va_mtime.tv_nsec = (time_t) -1;
136 vattr->va_ctime.tv_sec = (time_t) -1;
137 vattr->va_ctime.tv_nsec = (time_t) -1;
138 vattr->va_type = C_VNON;
139 vattr->va_fileid = -1;
140 vattr->va_gen = -1;
141 vattr->va_bytes = -1;
142 vattr->va_nlink = -1;
143 vattr->va_blocksize = -1;
144 vattr->va_rdev = -1;
145 vattr->va_flags = 0;
146
147 /* determine the type */
148#if 0
149 mode = iattr->ia_mode;
150 if ( S_ISDIR(mode) ) {
151 vattr->va_type = C_VDIR;
152 } else if ( S_ISREG(mode) ) {
153 vattr->va_type = C_VREG;
154 } else if ( S_ISLNK(mode) ) {
155 vattr->va_type = C_VLNK;
156 } else {
157 /* don't do others */
158 vattr->va_type = C_VNON;
159 }
160#endif
161
162 /* set those vattrs that need change */
163 valid = iattr->ia_valid;
164 if ( valid & ATTR_MODE ) {
165 vattr->va_mode = iattr->ia_mode;
166 }
167 if ( valid & ATTR_UID ) {
Eric W. Biedermand83f5902013-01-30 19:21:14 -0800168 vattr->va_uid = (vuid_t) from_kuid(&init_user_ns, iattr->ia_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 if ( valid & ATTR_GID ) {
Eric W. Biedermand83f5902013-01-30 19:21:14 -0800171 vattr->va_gid = (vgid_t) from_kgid(&init_user_ns, iattr->ia_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 if ( valid & ATTR_SIZE ) {
174 vattr->va_size = iattr->ia_size;
175 }
176 if ( valid & ATTR_ATIME ) {
177 vattr->va_atime = iattr->ia_atime;
178 }
179 if ( valid & ATTR_MTIME ) {
180 vattr->va_mtime = iattr->ia_mtime;
181 }
182 if ( valid & ATTR_CTIME ) {
183 vattr->va_ctime = iattr->ia_ctime;
184 }
185}
186