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