[PATCH] Add a driver for the Technisat Skystar2 DVB card
[linux-2.6/history.git] / fs / stat.c
blob2bce117d51d57f714f7836dcefc5e9b7f24db904
1 /*
2 * linux/fs/stat.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/config.h>
8 #include <linux/mm.h>
9 #include <linux/errno.h>
10 #include <linux/file.h>
11 #include <linux/smp_lock.h>
12 #include <linux/highuid.h>
13 #include <linux/fs.h>
14 #include <linux/namei.h>
15 #include <linux/security.h>
17 #include <asm/uaccess.h>
19 void generic_fillattr(struct inode *inode, struct kstat *stat)
21 stat->dev = inode->i_sb->s_dev;
22 stat->ino = inode->i_ino;
23 stat->mode = inode->i_mode;
24 stat->nlink = inode->i_nlink;
25 stat->uid = inode->i_uid;
26 stat->gid = inode->i_gid;
27 stat->rdev = kdev_t_to_nr(inode->i_rdev);
28 stat->atime = inode->i_atime;
29 stat->mtime = inode->i_mtime;
30 stat->ctime = inode->i_ctime;
31 stat->size = i_size_read(inode);
32 stat->blocks = inode->i_blocks;
33 stat->blksize = inode->i_blksize;
36 int vfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
38 struct inode *inode = dentry->d_inode;
39 int retval;
41 retval = security_inode_getattr(mnt, dentry);
42 if (retval)
43 return retval;
45 if (inode->i_op->getattr)
46 return inode->i_op->getattr(mnt, dentry, stat);
48 generic_fillattr(inode, stat);
49 if (!stat->blksize) {
50 struct super_block *s = inode->i_sb;
51 unsigned blocks;
52 blocks = (stat->size+s->s_blocksize-1) >> s->s_blocksize_bits;
53 stat->blocks = (s->s_blocksize / 512) * blocks;
54 stat->blksize = s->s_blocksize;
56 return 0;
59 int vfs_stat(char __user *name, struct kstat *stat)
61 struct nameidata nd;
62 int error;
64 error = user_path_walk(name, &nd);
65 if (!error) {
66 error = vfs_getattr(nd.mnt, nd.dentry, stat);
67 path_release(&nd);
69 return error;
72 int vfs_lstat(char __user *name, struct kstat *stat)
74 struct nameidata nd;
75 int error;
77 error = user_path_walk_link(name, &nd);
78 if (!error) {
79 error = vfs_getattr(nd.mnt, nd.dentry, stat);
80 path_release(&nd);
82 return error;
85 int vfs_fstat(unsigned int fd, struct kstat *stat)
87 struct file *f = fget(fd);
88 int error = -EBADF;
90 if (f) {
91 error = vfs_getattr(f->f_vfsmnt, f->f_dentry, stat);
92 fput(f);
94 return error;
97 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) \
98 && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__) \
99 && !defined(__arm__) && !defined(CONFIG_V850) && !defined(__powerpc64__)
102 * For backward compatibility? Maybe this should be moved
103 * into arch/i386 instead?
105 static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
107 static int warncount = 5;
108 struct __old_kernel_stat tmp;
110 if (warncount > 0) {
111 warncount--;
112 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
113 current->comm);
114 } else if (warncount < 0) {
115 /* it's laughable, but... */
116 warncount = 0;
119 tmp.st_dev = stat->dev;
120 tmp.st_ino = stat->ino;
121 tmp.st_mode = stat->mode;
122 tmp.st_nlink = stat->nlink;
123 SET_OLDSTAT_UID(tmp, stat->uid);
124 SET_OLDSTAT_GID(tmp, stat->gid);
125 tmp.st_rdev = stat->rdev;
126 #if BITS_PER_LONG == 32
127 if (stat->size > MAX_NON_LFS)
128 return -EOVERFLOW;
129 #endif
130 tmp.st_size = stat->size;
131 tmp.st_atime = stat->atime.tv_sec;
132 tmp.st_mtime = stat->mtime.tv_sec;
133 tmp.st_ctime = stat->ctime.tv_sec;
134 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
137 asmlinkage long sys_stat(char __user * filename, struct __old_kernel_stat __user * statbuf)
139 struct kstat stat;
140 int error = vfs_stat(filename, &stat);
142 if (!error)
143 error = cp_old_stat(&stat, statbuf);
145 return error;
147 asmlinkage long sys_lstat(char __user * filename, struct __old_kernel_stat __user * statbuf)
149 struct kstat stat;
150 int error = vfs_lstat(filename, &stat);
152 if (!error)
153 error = cp_old_stat(&stat, statbuf);
155 return error;
157 asmlinkage long sys_fstat(unsigned int fd, struct __old_kernel_stat __user * statbuf)
159 struct kstat stat;
160 int error = vfs_fstat(fd, &stat);
162 if (!error)
163 error = cp_old_stat(&stat, statbuf);
165 return error;
168 #endif
170 static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
172 struct stat tmp;
174 memset(&tmp, 0, sizeof(tmp));
175 tmp.st_dev = stat->dev;
176 tmp.st_ino = stat->ino;
177 tmp.st_mode = stat->mode;
178 tmp.st_nlink = stat->nlink;
179 SET_STAT_UID(tmp, stat->uid);
180 SET_STAT_GID(tmp, stat->gid);
181 tmp.st_rdev = stat->rdev;
182 #if BITS_PER_LONG == 32
183 if (stat->size > MAX_NON_LFS)
184 return -EOVERFLOW;
185 #endif
186 tmp.st_size = stat->size;
187 tmp.st_atime = stat->atime.tv_sec;
188 tmp.st_mtime = stat->mtime.tv_sec;
189 tmp.st_ctime = stat->ctime.tv_sec;
190 #ifdef STAT_HAVE_NSEC
191 tmp.st_atime_nsec = stat->atime.tv_nsec;
192 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
193 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
194 #endif
195 tmp.st_blocks = stat->blocks;
196 tmp.st_blksize = stat->blksize;
197 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
200 asmlinkage long sys_newstat(char __user * filename, struct stat __user * statbuf)
202 struct kstat stat;
203 int error = vfs_stat(filename, &stat);
205 if (!error)
206 error = cp_new_stat(&stat, statbuf);
208 return error;
210 asmlinkage long sys_newlstat(char __user * filename, struct stat __user * statbuf)
212 struct kstat stat;
213 int error = vfs_lstat(filename, &stat);
215 if (!error)
216 error = cp_new_stat(&stat, statbuf);
218 return error;
220 asmlinkage long sys_newfstat(unsigned int fd, struct stat __user * statbuf)
222 struct kstat stat;
223 int error = vfs_fstat(fd, &stat);
225 if (!error)
226 error = cp_new_stat(&stat, statbuf);
228 return error;
231 asmlinkage long sys_readlink(const char __user * path, char __user * buf, int bufsiz)
233 struct nameidata nd;
234 int error;
236 if (bufsiz <= 0)
237 return -EINVAL;
239 error = user_path_walk_link(path, &nd);
240 if (!error) {
241 struct inode * inode = nd.dentry->d_inode;
243 error = -EINVAL;
244 if (inode->i_op && inode->i_op->readlink) {
245 error = security_inode_readlink(nd.dentry);
246 if (!error) {
247 update_atime(inode);
248 error = inode->i_op->readlink(nd.dentry, buf, bufsiz);
251 path_release(&nd);
253 return error;
257 /* ---------- LFS-64 ----------- */
258 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips64) && !defined(__x86_64__) && !defined(CONFIG_ARCH_S390X)
260 static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
262 struct stat64 tmp;
264 memset(&tmp, 0, sizeof(struct stat64));
265 tmp.st_dev = stat->dev;
266 tmp.st_ino = stat->ino;
267 #ifdef STAT64_HAS_BROKEN_ST_INO
268 tmp.__st_ino = stat->ino;
269 #endif
270 tmp.st_mode = stat->mode;
271 tmp.st_nlink = stat->nlink;
272 tmp.st_uid = stat->uid;
273 tmp.st_gid = stat->gid;
274 tmp.st_rdev = stat->rdev;
275 tmp.st_atime = stat->atime.tv_sec;
276 tmp.st_atime_nsec = stat->atime.tv_nsec;
277 tmp.st_mtime = stat->mtime.tv_sec;
278 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
279 tmp.st_ctime = stat->ctime.tv_sec;
280 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
281 tmp.st_size = stat->size;
282 tmp.st_blocks = stat->blocks;
283 tmp.st_blksize = stat->blksize;
284 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
287 asmlinkage long sys_stat64(char __user * filename, struct stat64 __user * statbuf, long flags)
289 struct kstat stat;
290 int error = vfs_stat(filename, &stat);
292 if (!error)
293 error = cp_new_stat64(&stat, statbuf);
295 return error;
297 asmlinkage long sys_lstat64(char __user * filename, struct stat64 __user * statbuf, long flags)
299 struct kstat stat;
300 int error = vfs_lstat(filename, &stat);
302 if (!error)
303 error = cp_new_stat64(&stat, statbuf);
305 return error;
307 asmlinkage long sys_fstat64(unsigned long fd, struct stat64 __user * statbuf, long flags)
309 struct kstat stat;
310 int error = vfs_fstat(fd, &stat);
312 if (!error)
313 error = cp_new_stat64(&stat, statbuf);
315 return error;
318 #endif /* LFS-64 */
320 void inode_add_bytes(struct inode *inode, loff_t bytes)
322 spin_lock(&inode->i_lock);
323 inode->i_blocks += bytes >> 9;
324 bytes &= 511;
325 inode->i_bytes += bytes;
326 if (inode->i_bytes >= 512) {
327 inode->i_blocks++;
328 inode->i_bytes -= 512;
330 spin_unlock(&inode->i_lock);
333 void inode_sub_bytes(struct inode *inode, loff_t bytes)
335 spin_lock(&inode->i_lock);
336 inode->i_blocks -= bytes >> 9;
337 bytes &= 511;
338 if (inode->i_bytes < bytes) {
339 inode->i_blocks--;
340 inode->i_bytes += 512;
342 inode->i_bytes -= bytes;
343 spin_unlock(&inode->i_lock);
346 loff_t inode_get_bytes(struct inode *inode)
348 loff_t ret;
350 spin_lock(&inode->i_lock);
351 ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
352 spin_unlock(&inode->i_lock);
353 return ret;
356 void inode_set_bytes(struct inode *inode, loff_t bytes)
358 inode->i_blocks = bytes >> 9;
359 inode->i_bytes = bytes & 511;