Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / fs / open.c
1 /*
2  *  linux/fs/open.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <linux/string.h>
8 #include <linux/mm.h>
9 #include <linux/utime.h>
10 #include <linux/file.h>
11 #include <linux/smp_lock.h>
12 #include <linux/quotaops.h>
13 #include <linux/fsnotify.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/namei.h>
18 #include <linux/backing-dev.h>
19 #include <linux/capability.h>
20 #include <linux/security.h>
21 #include <linux/mount.h>
22 #include <linux/vfs.h>
23 #include <linux/fcntl.h>
24 #include <asm/uaccess.h>
25 #include <linux/fs.h>
26 #include <linux/personality.h>
27 #include <linux/pagemap.h>
28 #include <linux/syscalls.h>
29 #include <linux/rcupdate.h>
30 #include <linux/audit.h>
31 #include <linux/vs_limit.h>
32 #include <linux/vs_dlimit.h>
33 #include <linux/vserver/xid.h>
34
35 #include <asm/unistd.h>
36
37 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
38 {
39         int retval = -ENODEV;
40
41         if (sb) {
42                 retval = -ENOSYS;
43                 if (sb->s_op->statfs) {
44                         memset(buf, 0, sizeof(*buf));
45                         retval = security_sb_statfs(sb);
46                         if (retval)
47                                 return retval;
48                         retval = sb->s_op->statfs(sb, buf);
49                         if (retval == 0 && buf->f_frsize == 0)
50                                 buf->f_frsize = buf->f_bsize;
51                 }
52                 if (!vx_check(0, VX_ADMIN|VX_WATCH))
53                         vx_vsi_statfs(sb, buf);
54         }
55         return retval;
56 }
57
58 EXPORT_SYMBOL(vfs_statfs);
59
60 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
61 {
62         struct kstatfs st;
63         int retval;
64
65         retval = vfs_statfs(sb, &st);
66         if (retval)
67                 return retval;
68
69         if (sizeof(*buf) == sizeof(st))
70                 memcpy(buf, &st, sizeof(st));
71         else {
72                 if (sizeof buf->f_blocks == 4) {
73                         if ((st.f_blocks | st.f_bfree | st.f_bavail) &
74                             0xffffffff00000000ULL)
75                                 return -EOVERFLOW;
76                         /*
77                          * f_files and f_ffree may be -1; it's okay to stuff
78                          * that into 32 bits
79                          */
80                         if (st.f_files != -1 &&
81                             (st.f_files & 0xffffffff00000000ULL))
82                                 return -EOVERFLOW;
83                         if (st.f_ffree != -1 &&
84                             (st.f_ffree & 0xffffffff00000000ULL))
85                                 return -EOVERFLOW;
86                 }
87
88                 buf->f_type = st.f_type;
89                 buf->f_bsize = st.f_bsize;
90                 buf->f_blocks = st.f_blocks;
91                 buf->f_bfree = st.f_bfree;
92                 buf->f_bavail = st.f_bavail;
93                 buf->f_files = st.f_files;
94                 buf->f_ffree = st.f_ffree;
95                 buf->f_fsid = st.f_fsid;
96                 buf->f_namelen = st.f_namelen;
97                 buf->f_frsize = st.f_frsize;
98                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
99         }
100         return 0;
101 }
102
103 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
104 {
105         struct kstatfs st;
106         int retval;
107
108         retval = vfs_statfs(sb, &st);
109         if (retval)
110                 return retval;
111
112         if (sizeof(*buf) == sizeof(st))
113                 memcpy(buf, &st, sizeof(st));
114         else {
115                 buf->f_type = st.f_type;
116                 buf->f_bsize = st.f_bsize;
117                 buf->f_blocks = st.f_blocks;
118                 buf->f_bfree = st.f_bfree;
119                 buf->f_bavail = st.f_bavail;
120                 buf->f_files = st.f_files;
121                 buf->f_ffree = st.f_ffree;
122                 buf->f_fsid = st.f_fsid;
123                 buf->f_namelen = st.f_namelen;
124                 buf->f_frsize = st.f_frsize;
125                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
126         }
127         return 0;
128 }
129
130 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
131 {
132         struct nameidata nd;
133         int error;
134
135         error = user_path_walk(path, &nd);
136         if (!error) {
137                 struct statfs tmp;
138                 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
139                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
140                         error = -EFAULT;
141                 path_release(&nd);
142         }
143         return error;
144 }
145
146
147 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
148 {
149         struct nameidata nd;
150         long error;
151
152         if (sz != sizeof(*buf))
153                 return -EINVAL;
154         error = user_path_walk(path, &nd);
155         if (!error) {
156                 struct statfs64 tmp;
157                 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
158                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
159                         error = -EFAULT;
160                 path_release(&nd);
161         }
162         return error;
163 }
164
165
166 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
167 {
168         struct file * file;
169         struct statfs tmp;
170         int error;
171
172         error = -EBADF;
173         file = fget(fd);
174         if (!file)
175                 goto out;
176         error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
177         if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
178                 error = -EFAULT;
179         fput(file);
180 out:
181         return error;
182 }
183
184 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
185 {
186         struct file * file;
187         struct statfs64 tmp;
188         int error;
189
190         if (sz != sizeof(*buf))
191                 return -EINVAL;
192
193         error = -EBADF;
194         file = fget(fd);
195         if (!file)
196                 goto out;
197         error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
198         if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
199                 error = -EFAULT;
200         fput(file);
201 out:
202         return error;
203 }
204
205 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
206         struct file *filp)
207 {
208         int err;
209         struct iattr newattrs;
210
211         /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
212         if (length < 0)
213                 return -EINVAL;
214
215         newattrs.ia_size = length;
216         newattrs.ia_valid = ATTR_SIZE | time_attrs;
217         if (filp) {
218                 newattrs.ia_file = filp;
219                 newattrs.ia_valid |= ATTR_FILE;
220         }
221
222         mutex_lock(&dentry->d_inode->i_mutex);
223         err = notify_change(dentry, &newattrs);
224         mutex_unlock(&dentry->d_inode->i_mutex);
225         return err;
226 }
227
228 static long do_sys_truncate(const char __user * path, loff_t length)
229 {
230         struct nameidata nd;
231         struct inode * inode;
232         int error;
233
234         error = -EINVAL;
235         if (length < 0) /* sorry, but loff_t says... */
236                 goto out;
237
238         error = user_path_walk(path, &nd);
239         if (error)
240                 goto out;
241         inode = nd.dentry->d_inode;
242
243         /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
244         error = -EISDIR;
245         if (S_ISDIR(inode->i_mode))
246                 goto dput_and_out;
247
248         error = -EINVAL;
249         if (!S_ISREG(inode->i_mode))
250                 goto dput_and_out;
251
252         error = vfs_permission(&nd, MAY_WRITE);
253         if (error)
254                 goto dput_and_out;
255
256         error = -EROFS;
257         if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
258                 goto dput_and_out;
259
260         error = -EPERM;
261         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
262                 goto dput_and_out;
263
264         /*
265          * Make sure that there are no leases.
266          */
267         error = break_lease(inode, FMODE_WRITE);
268         if (error)
269                 goto dput_and_out;
270
271         error = get_write_access(inode);
272         if (error)
273                 goto dput_and_out;
274
275         error = locks_verify_truncate(inode, NULL, length);
276         if (!error) {
277                 DQUOT_INIT(inode);
278                 error = do_truncate(nd.dentry, length, 0, NULL);
279         }
280         put_write_access(inode);
281
282 dput_and_out:
283         path_release(&nd);
284 out:
285         return error;
286 }
287
288 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
289 {
290         /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
291         return do_sys_truncate(path, (long)length);
292 }
293
294 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
295 {
296         struct inode * inode;
297         struct dentry *dentry;
298         struct file * file;
299         int error;
300
301         error = -EINVAL;
302         if (length < 0)
303                 goto out;
304         error = -EBADF;
305         file = fget(fd);
306         if (!file)
307                 goto out;
308
309         /* explicitly opened as large or we are on 64-bit box */
310         if (file->f_flags & O_LARGEFILE)
311                 small = 0;
312
313         dentry = file->f_dentry;
314         inode = dentry->d_inode;
315         error = -EINVAL;
316         if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
317                 goto out_putf;
318
319         error = -EINVAL;
320         /* Cannot ftruncate over 2^31 bytes without large file support */
321         if (small && length > MAX_NON_LFS)
322                 goto out_putf;
323
324         error = -EPERM;
325         if (IS_APPEND(inode))
326                 goto out_putf;
327
328         error = locks_verify_truncate(inode, file, length);
329         if (!error)
330                 error = do_truncate(dentry, length, 0, file);
331 out_putf:
332         fput(file);
333 out:
334         return error;
335 }
336
337 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
338 {
339         long ret = do_sys_ftruncate(fd, length, 1);
340         /* avoid REGPARM breakage on x86: */
341         prevent_tail_call(ret);
342         return ret;
343 }
344
345 /* LFS versions of truncate are only needed on 32 bit machines */
346 #if BITS_PER_LONG == 32
347 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
348 {
349         return do_sys_truncate(path, length);
350 }
351
352 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
353 {
354         long ret = do_sys_ftruncate(fd, length, 0);
355         /* avoid REGPARM breakage on x86: */
356         prevent_tail_call(ret);
357         return ret;
358 }
359 #endif
360
361 #ifdef __ARCH_WANT_SYS_UTIME
362
363 /*
364  * sys_utime() can be implemented in user-level using sys_utimes().
365  * Is this for backwards compatibility?  If so, why not move it
366  * into the appropriate arch directory (for those architectures that
367  * need it).
368  */
369
370 /* If times==NULL, set access and modification to current time,
371  * must be owner or have write permission.
372  * Else, update from *times, must be owner or super user.
373  */
374 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
375 {
376         int error;
377         struct nameidata nd;
378         struct inode * inode;
379         struct iattr newattrs;
380
381         error = user_path_walk(filename, &nd);
382         if (error)
383                 goto out;
384         inode = nd.dentry->d_inode;
385
386         error = -EROFS;
387         if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
388                 goto dput_and_out;
389
390         /* Don't worry, the checks are done in inode_change_ok() */
391         newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
392         if (times) {
393                 error = -EPERM;
394                 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
395                         goto dput_and_out;
396
397                 error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
398                 newattrs.ia_atime.tv_nsec = 0;
399                 if (!error)
400                         error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
401                 newattrs.ia_mtime.tv_nsec = 0;
402                 if (error)
403                         goto dput_and_out;
404
405                 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
406         } else {
407                 error = -EACCES;
408                 if (IS_IMMUTABLE(inode))
409                         goto dput_and_out;
410
411                 if (current->fsuid != inode->i_uid &&
412                     (error = vfs_permission(&nd, MAY_WRITE)) != 0)
413                         goto dput_and_out;
414         }
415         mutex_lock(&inode->i_mutex);
416         error = notify_change(nd.dentry, &newattrs);
417         mutex_unlock(&inode->i_mutex);
418 dput_and_out:
419         path_release(&nd);
420 out:
421         return error;
422 }
423
424 #endif
425
426 /* If times==NULL, set access and modification to current time,
427  * must be owner or have write permission.
428  * Else, update from *times, must be owner or super user.
429  */
430 long do_utimes(int dfd, char __user *filename, struct timeval *times)
431 {
432         int error;
433         struct nameidata nd;
434         struct inode * inode;
435         struct iattr newattrs;
436
437         error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
438
439         if (error)
440                 goto out;
441         inode = nd.dentry->d_inode;
442
443         error = -EROFS;
444         if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
445                 goto dput_and_out;
446
447         /* Don't worry, the checks are done in inode_change_ok() */
448         newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
449         if (times) {
450                 error = -EPERM;
451                 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
452                         goto dput_and_out;
453
454                 newattrs.ia_atime.tv_sec = times[0].tv_sec;
455                 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
456                 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
457                 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
458                 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
459         } else {
460                 error = -EACCES;
461                 if (IS_IMMUTABLE(inode))
462                         goto dput_and_out;
463
464                 if (current->fsuid != inode->i_uid &&
465                     (error = vfs_permission(&nd, MAY_WRITE)) != 0)
466                         goto dput_and_out;
467         }
468         mutex_lock(&inode->i_mutex);
469         error = notify_change(nd.dentry, &newattrs);
470         mutex_unlock(&inode->i_mutex);
471 dput_and_out:
472         path_release(&nd);
473 out:
474         return error;
475 }
476
477 asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
478 {
479         struct timeval times[2];
480
481         if (utimes && copy_from_user(&times, utimes, sizeof(times)))
482                 return -EFAULT;
483         return do_utimes(dfd, filename, utimes ? times : NULL);
484 }
485
486 asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
487 {
488         return sys_futimesat(AT_FDCWD, filename, utimes);
489 }
490
491
492 /*
493  * access() needs to use the real uid/gid, not the effective uid/gid.
494  * We do this by temporarily clearing all FS-related capabilities and
495  * switching the fsuid/fsgid around to the real ones.
496  */
497 asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
498 {
499         struct nameidata nd;
500         int old_fsuid, old_fsgid;
501         kernel_cap_t old_cap;
502         int res;
503
504         if (mode & ~S_IRWXO)    /* where's F_OK, X_OK, W_OK, R_OK? */
505                 return -EINVAL;
506
507         old_fsuid = current->fsuid;
508         old_fsgid = current->fsgid;
509         old_cap = current->cap_effective;
510
511         current->fsuid = current->uid;
512         current->fsgid = current->gid;
513
514         /*
515          * Clear the capabilities if we switch to a non-root user
516          *
517          * FIXME: There is a race here against sys_capset.  The
518          * capabilities can change yet we will restore the old
519          * value below.  We should hold task_capabilities_lock,
520          * but we cannot because user_path_walk can sleep.
521          */
522         if (current->uid)
523                 cap_clear(current->cap_effective);
524         else
525                 current->cap_effective = current->cap_permitted;
526
527         res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
528         if (!res) {
529                 res = vfs_permission(&nd, mode);
530                 /* SuS v2 requires we report a read only fs too */
531                 if(!res && (mode & S_IWOTH)
532                    && (IS_RDONLY(nd.dentry->d_inode) || MNT_IS_RDONLY(nd.mnt))
533                    && !special_file(nd.dentry->d_inode->i_mode))
534                         res = -EROFS;
535                 path_release(&nd);
536         }
537
538         current->fsuid = old_fsuid;
539         current->fsgid = old_fsgid;
540         current->cap_effective = old_cap;
541
542         return res;
543 }
544
545 asmlinkage long sys_access(const char __user *filename, int mode)
546 {
547         return sys_faccessat(AT_FDCWD, filename, mode);
548 }
549
550 asmlinkage long sys_chdir(const char __user * filename)
551 {
552         struct nameidata nd;
553         int error;
554
555         error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
556         if (error)
557                 goto out;
558
559         error = vfs_permission(&nd, MAY_EXEC);
560         if (error)
561                 goto dput_and_out;
562
563         set_fs_pwd(current->fs, nd.mnt, nd.dentry);
564
565 dput_and_out:
566         path_release(&nd);
567 out:
568         return error;
569 }
570
571 EXPORT_SYMBOL_GPL(sys_chdir);
572
573 asmlinkage long sys_fchdir(unsigned int fd)
574 {
575         struct file *file;
576         struct dentry *dentry;
577         struct inode *inode;
578         struct vfsmount *mnt;
579         int error;
580
581         error = -EBADF;
582         file = fget(fd);
583         if (!file)
584                 goto out;
585
586         dentry = file->f_dentry;
587         mnt = file->f_vfsmnt;
588         inode = dentry->d_inode;
589
590         error = -ENOTDIR;
591         if (!S_ISDIR(inode->i_mode))
592                 goto out_putf;
593
594         error = file_permission(file, MAY_EXEC);
595         if (!error)
596                 set_fs_pwd(current->fs, mnt, dentry);
597 out_putf:
598         fput(file);
599 out:
600         return error;
601 }
602
603 asmlinkage long sys_chroot(const char __user * filename)
604 {
605         struct nameidata nd;
606         int error;
607
608         error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
609         if (error)
610                 goto out;
611
612         error = vfs_permission(&nd, MAY_EXEC);
613         if (error)
614                 goto dput_and_out;
615
616         error = -EPERM;
617         if (!capable(CAP_SYS_CHROOT))
618                 goto dput_and_out;
619
620         set_fs_root(current->fs, nd.mnt, nd.dentry);
621         set_fs_altroot();
622         error = 0;
623 dput_and_out:
624         path_release(&nd);
625 out:
626         return error;
627 }
628
629 EXPORT_SYMBOL_GPL(sys_chroot);
630
631 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
632 {
633         struct inode * inode;
634         struct dentry * dentry;
635         struct file * file;
636         int err = -EBADF;
637         struct iattr newattrs;
638
639         file = fget(fd);
640         if (!file)
641                 goto out;
642
643         dentry = file->f_dentry;
644         inode = dentry->d_inode;
645
646         audit_inode(NULL, inode, 0);
647
648         err = -EROFS;
649         if (IS_RDONLY(inode) || MNT_IS_RDONLY(file->f_vfsmnt))
650                 goto out_putf;
651         err = -EPERM;
652         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
653                 goto out_putf;
654         mutex_lock(&inode->i_mutex);
655         if (mode == (mode_t) -1)
656                 mode = inode->i_mode;
657         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
658         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
659         err = notify_change(dentry, &newattrs);
660         mutex_unlock(&inode->i_mutex);
661
662 out_putf:
663         fput(file);
664 out:
665         return err;
666 }
667
668 asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
669                              mode_t mode)
670 {
671         struct nameidata nd;
672         struct inode * inode;
673         int error;
674         struct iattr newattrs;
675
676         error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
677         if (error)
678                 goto out;
679         inode = nd.dentry->d_inode;
680
681         error = -EROFS;
682         if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
683                 goto dput_and_out;
684
685         error = -EPERM;
686         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
687                 goto dput_and_out;
688
689         mutex_lock(&inode->i_mutex);
690         if (mode == (mode_t) -1)
691                 mode = inode->i_mode;
692         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
693         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
694         error = notify_change(nd.dentry, &newattrs);
695         mutex_unlock(&inode->i_mutex);
696
697 dput_and_out:
698         path_release(&nd);
699 out:
700         return error;
701 }
702
703 asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
704 {
705         return sys_fchmodat(AT_FDCWD, filename, mode);
706 }
707
708 static int chown_common(struct dentry *dentry, struct vfsmount *mnt,
709         uid_t user, gid_t group)
710 {
711         struct inode * inode;
712         int error;
713         struct iattr newattrs;
714
715         error = -ENOENT;
716         if (!(inode = dentry->d_inode)) {
717                 printk(KERN_ERR "chown_common: NULL inode\n");
718                 goto out;
719         }
720         error = -EROFS;
721         if (IS_RDONLY(inode) || MNT_IS_RDONLY(mnt))
722                 goto out;
723         error = -EPERM;
724         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
725                 goto out;
726         newattrs.ia_valid =  ATTR_CTIME;
727         if (user != (uid_t) -1) {
728                 newattrs.ia_valid |= ATTR_UID;
729                 newattrs.ia_uid = vx_map_uid(user);
730         }
731         if (group != (gid_t) -1) {
732                 newattrs.ia_valid |= ATTR_GID;
733                 newattrs.ia_gid = vx_map_gid(group);
734         }
735         if (!S_ISDIR(inode->i_mode))
736                 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
737         mutex_lock(&inode->i_mutex);
738         error = notify_change(dentry, &newattrs);
739         mutex_unlock(&inode->i_mutex);
740 out:
741         return error;
742 }
743
744 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
745 {
746         struct nameidata nd;
747         int error;
748
749         error = user_path_walk(filename, &nd);
750         if (!error) {
751                 error = chown_common(nd.dentry, nd.mnt, user, group);
752                 path_release(&nd);
753         }
754         return error;
755 }
756
757 asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
758                              gid_t group, int flag)
759 {
760         struct nameidata nd;
761         int error = -EINVAL;
762         int follow;
763
764         if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
765                 goto out;
766
767         follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
768         error = __user_walk_fd(dfd, filename, follow, &nd);
769         if (!error) {
770                 error = chown_common(nd.dentry, nd.mnt, user, group);
771                 path_release(&nd);
772         }
773 out:
774         return error;
775 }
776
777 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
778 {
779         struct nameidata nd;
780         int error;
781
782         error = user_path_walk_link(filename, &nd);
783         if (!error) {
784                 error = chown_common(nd.dentry, nd.mnt, user, group);
785                 path_release(&nd);
786         }
787         return error;
788 }
789
790
791 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
792 {
793         struct file * file;
794         int error = -EBADF;
795
796         file = fget(fd);
797         if (file) {
798                 struct dentry * dentry;
799                 dentry = file->f_dentry;
800                 audit_inode(NULL, dentry->d_inode, 0);
801                 error = chown_common(dentry, file->f_vfsmnt, user, group);
802                 fput(file);
803         }
804         return error;
805 }
806
807 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
808                                         int flags, struct file *f,
809                                         int (*open)(struct inode *, struct file *))
810 {
811         struct inode *inode;
812         int error;
813
814         f->f_flags = flags;
815         f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
816                                 FMODE_PREAD | FMODE_PWRITE;
817         inode = dentry->d_inode;
818         if (f->f_mode & FMODE_WRITE) {
819                 error = get_write_access(inode);
820                 if (error)
821                         goto cleanup_file;
822         }
823
824         f->f_mapping = inode->i_mapping;
825         f->f_dentry = dentry;
826         f->f_vfsmnt = mnt;
827         f->f_pos = 0;
828         f->f_op = fops_get(inode->i_fop);
829         file_move(f, &inode->i_sb->s_files);
830
831         if (!open && f->f_op)
832                 open = f->f_op->open;
833         if (open) {
834                 error = open(inode, f);
835                 if (error)
836                         goto cleanup_all;
837         }
838
839         f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
840
841         file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
842
843         /* NB: we're sure to have correct a_ops only after f_op->open */
844         if (f->f_flags & O_DIRECT) {
845                 if (!f->f_mapping->a_ops ||
846                     ((!f->f_mapping->a_ops->direct_IO) &&
847                     (!f->f_mapping->a_ops->get_xip_page))) {
848                         fput(f);
849                         f = ERR_PTR(-EINVAL);
850                 }
851         }
852
853         return f;
854
855 cleanup_all:
856         fops_put(f->f_op);
857         if (f->f_mode & FMODE_WRITE)
858                 put_write_access(inode);
859         file_kill(f);
860         f->f_dentry = NULL;
861         f->f_vfsmnt = NULL;
862 cleanup_file:
863         put_filp(f);
864         dput(dentry);
865         mntput(mnt);
866         return ERR_PTR(error);
867 }
868
869 /*
870  * Note that while the flag value (low two bits) for sys_open means:
871  *      00 - read-only
872  *      01 - write-only
873  *      10 - read-write
874  *      11 - special
875  * it is changed into
876  *      00 - no permissions needed
877  *      01 - read-permission
878  *      10 - write-permission
879  *      11 - read-write
880  * for the internal routines (ie open_namei()/follow_link() etc). 00 is
881  * used by symlinks.
882  */
883 static struct file *do_filp_open(int dfd, const char *filename, int flags,
884                                  int mode)
885 {
886         int namei_flags, error;
887         struct nameidata nd;
888
889         namei_flags = flags;
890         if ((namei_flags+1) & O_ACCMODE)
891                 namei_flags++;
892
893         error = open_namei(dfd, filename, namei_flags, mode, &nd);
894         if (!error)
895                 return nameidata_to_filp(&nd, flags);
896
897         return ERR_PTR(error);
898 }
899
900 struct file *filp_open(const char *filename, int flags, int mode)
901 {
902         return do_filp_open(AT_FDCWD, filename, flags, mode);
903 }
904 EXPORT_SYMBOL(filp_open);
905
906 /**
907  * lookup_instantiate_filp - instantiates the open intent filp
908  * @nd: pointer to nameidata
909  * @dentry: pointer to dentry
910  * @open: open callback
911  *
912  * Helper for filesystems that want to use lookup open intents and pass back
913  * a fully instantiated struct file to the caller.
914  * This function is meant to be called from within a filesystem's
915  * lookup method.
916  * Beware of calling it for non-regular files! Those ->open methods might block
917  * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
918  * leading to a deadlock, as nobody can open that fifo anymore, because
919  * another process to open fifo will block on locked parent when doing lookup).
920  * Note that in case of error, nd->intent.open.file is destroyed, but the
921  * path information remains valid.
922  * If the open callback is set to NULL, then the standard f_op->open()
923  * filesystem callback is substituted.
924  */
925 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
926                 int (*open)(struct inode *, struct file *))
927 {
928         if (IS_ERR(nd->intent.open.file))
929                 goto out;
930         if (IS_ERR(dentry))
931                 goto out_err;
932         nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
933                                              nd->intent.open.flags - 1,
934                                              nd->intent.open.file,
935                                              open);
936 out:
937         return nd->intent.open.file;
938 out_err:
939         release_open_intent(nd);
940         nd->intent.open.file = (struct file *)dentry;
941         goto out;
942 }
943 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
944
945 /**
946  * nameidata_to_filp - convert a nameidata to an open filp.
947  * @nd: pointer to nameidata
948  * @flags: open flags
949  *
950  * Note that this function destroys the original nameidata
951  */
952 struct file *nameidata_to_filp(struct nameidata *nd, int flags)
953 {
954         struct file *filp;
955
956         /* Pick up the filp from the open intent */
957         filp = nd->intent.open.file;
958         /* Has the filesystem initialised the file for us? */
959         if (filp->f_dentry == NULL)
960                 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
961         else
962                 path_release(nd);
963         return filp;
964 }
965
966 /*
967  * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
968  * error.
969  */
970 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
971 {
972         int error;
973         struct file *f;
974
975         error = -ENFILE;
976         f = get_empty_filp();
977         if (f == NULL) {
978                 dput(dentry);
979                 mntput(mnt);
980                 return ERR_PTR(error);
981         }
982
983         return __dentry_open(dentry, mnt, flags, f, NULL);
984 }
985 EXPORT_SYMBOL(dentry_open);
986
987 /*
988  * Find an empty file descriptor entry, and mark it busy.
989  */
990 int get_unused_fd(void)
991 {
992         struct files_struct * files = current->files;
993         int fd, error;
994         struct fdtable *fdt;
995
996         error = -EMFILE;
997         spin_lock(&files->file_lock);
998
999 repeat:
1000         fdt = files_fdtable(files);
1001         fd = find_next_zero_bit(fdt->open_fds->fds_bits,
1002                                 fdt->max_fdset,
1003                                 files->next_fd);
1004
1005         /*
1006          * N.B. For clone tasks sharing a files structure, this test
1007          * will limit the total number of files that can be opened.
1008          */
1009         if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
1010                 goto out;
1011
1012         /* Do we need to expand the fd array or fd set?  */
1013         error = expand_files(files, fd);
1014         if (error < 0)
1015                 goto out;
1016
1017         if (error) {
1018                 /*
1019                  * If we needed to expand the fs array we
1020                  * might have blocked - try again.
1021                  */
1022                 error = -EMFILE;
1023                 goto repeat;
1024         }
1025
1026         FD_SET(fd, fdt->open_fds);
1027         FD_CLR(fd, fdt->close_on_exec);
1028         files->next_fd = fd + 1;
1029         vx_openfd_inc(fd);
1030 #if 1
1031         /* Sanity check */
1032         if (fdt->fd[fd] != NULL) {
1033                 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
1034                 fdt->fd[fd] = NULL;
1035         }
1036 #endif
1037         error = fd;
1038
1039 out:
1040         spin_unlock(&files->file_lock);
1041         return error;
1042 }
1043
1044 EXPORT_SYMBOL(get_unused_fd);
1045
1046 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
1047 {
1048         struct fdtable *fdt = files_fdtable(files);
1049         __FD_CLR(fd, fdt->open_fds);
1050         if (fd < files->next_fd)
1051                 files->next_fd = fd;
1052         vx_openfd_dec(fd);
1053 }
1054
1055 void fastcall put_unused_fd(unsigned int fd)
1056 {
1057         struct files_struct *files = current->files;
1058         spin_lock(&files->file_lock);
1059         __put_unused_fd(files, fd);
1060         spin_unlock(&files->file_lock);
1061 }
1062
1063 EXPORT_SYMBOL(put_unused_fd);
1064
1065 /*
1066  * Install a file pointer in the fd array.
1067  *
1068  * The VFS is full of places where we drop the files lock between
1069  * setting the open_fds bitmap and installing the file in the file
1070  * array.  At any such point, we are vulnerable to a dup2() race
1071  * installing a file in the array before us.  We need to detect this and
1072  * fput() the struct file we are about to overwrite in this case.
1073  *
1074  * It should never happen - if we allow dup2() do it, _really_ bad things
1075  * will follow.
1076  */
1077
1078 void fastcall fd_install(unsigned int fd, struct file * file)
1079 {
1080         struct files_struct *files = current->files;
1081         struct fdtable *fdt;
1082         spin_lock(&files->file_lock);
1083         fdt = files_fdtable(files);
1084         BUG_ON(fdt->fd[fd] != NULL);
1085         rcu_assign_pointer(fdt->fd[fd], file);
1086         spin_unlock(&files->file_lock);
1087 }
1088
1089 EXPORT_SYMBOL(fd_install);
1090
1091 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
1092 {
1093         char *tmp = getname(filename);
1094         int fd = PTR_ERR(tmp);
1095
1096         if (!IS_ERR(tmp)) {
1097                 fd = get_unused_fd();
1098                 if (fd >= 0) {
1099                         struct file *f = do_filp_open(dfd, tmp, flags, mode);
1100                         if (IS_ERR(f)) {
1101                                 put_unused_fd(fd);
1102                                 fd = PTR_ERR(f);
1103                         } else {
1104                                 fsnotify_open(f->f_dentry);
1105                                 fd_install(fd, f);
1106                         }
1107                 }
1108                 putname(tmp);
1109         }
1110         return fd;
1111 }
1112
1113 asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1114 {
1115         long ret;
1116
1117         if (force_o_largefile())
1118                 flags |= O_LARGEFILE;
1119
1120         ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1121         /* avoid REGPARM breakage on x86: */
1122         prevent_tail_call(ret);
1123         return ret;
1124 }
1125
1126 asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1127                            int mode)
1128 {
1129         long ret;
1130
1131         if (force_o_largefile())
1132                 flags |= O_LARGEFILE;
1133
1134         ret = do_sys_open(dfd, filename, flags, mode);
1135         /* avoid REGPARM breakage on x86: */
1136         prevent_tail_call(ret);
1137         return ret;
1138 }
1139
1140 #ifndef __alpha__
1141
1142 /*
1143  * For backward compatibility?  Maybe this should be moved
1144  * into arch/i386 instead?
1145  */
1146 asmlinkage long sys_creat(const char __user * pathname, int mode)
1147 {
1148         return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1149 }
1150
1151 #endif
1152
1153 /*
1154  * "id" is the POSIX thread ID. We use the
1155  * files pointer for this..
1156  */
1157 int filp_close(struct file *filp, fl_owner_t id)
1158 {
1159         int retval = 0;
1160
1161         if (!file_count(filp)) {
1162                 printk(KERN_ERR "VFS: Close: file count is 0\n");
1163                 return 0;
1164         }
1165
1166         if (filp->f_op && filp->f_op->flush)
1167                 retval = filp->f_op->flush(filp);
1168
1169         dnotify_flush(filp, id);
1170         locks_remove_posix(filp, id);
1171         fput(filp);
1172         return retval;
1173 }
1174
1175 EXPORT_SYMBOL(filp_close);
1176
1177 /*
1178  * Careful here! We test whether the file pointer is NULL before
1179  * releasing the fd. This ensures that one clone task can't release
1180  * an fd while another clone is opening it.
1181  */
1182 asmlinkage long sys_close(unsigned int fd)
1183 {
1184         struct file * filp;
1185         struct files_struct *files = current->files;
1186         struct fdtable *fdt;
1187
1188         spin_lock(&files->file_lock);
1189         fdt = files_fdtable(files);
1190         if (fd >= fdt->max_fds)
1191                 goto out_unlock;
1192         filp = fdt->fd[fd];
1193         if (!filp)
1194                 goto out_unlock;
1195         rcu_assign_pointer(fdt->fd[fd], NULL);
1196         FD_CLR(fd, fdt->close_on_exec);
1197         __put_unused_fd(files, fd);
1198         spin_unlock(&files->file_lock);
1199         return filp_close(filp, files);
1200
1201 out_unlock:
1202         spin_unlock(&files->file_lock);
1203         return -EBADF;
1204 }
1205
1206 EXPORT_SYMBOL(sys_close);
1207
1208 /*
1209  * This routine simulates a hangup on the tty, to arrange that users
1210  * are given clean terminals at login time.
1211  */
1212 asmlinkage long sys_vhangup(void)
1213 {
1214         if (capable(CAP_SYS_TTY_CONFIG)) {
1215                 tty_vhangup(current->signal->tty);
1216                 return 0;
1217         }
1218         return -EPERM;
1219 }
1220
1221 /*
1222  * Called when an inode is about to be open.
1223  * We use this to disallow opening large files on 32bit systems if
1224  * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
1225  * on this flag in sys_open.
1226  */
1227 int generic_file_open(struct inode * inode, struct file * filp)
1228 {
1229         if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1230                 return -EFBIG;
1231         return 0;
1232 }
1233
1234 EXPORT_SYMBOL(generic_file_open);
1235
1236 /*
1237  * This is used by subsystems that don't want seekable
1238  * file descriptors
1239  */
1240 int nonseekable_open(struct inode *inode, struct file *filp)
1241 {
1242         filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1243         return 0;
1244 }
1245
1246 EXPORT_SYMBOL(nonseekable_open);