OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [fs/] [namei.c] - Blame information for rev 1777

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1627 jcastillo
/*
2
 *  linux/fs/namei.c
3
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
5
 */
6
 
7
/*
8
 * Some corrections by tytso.
9
 *
10
 * Aug 97 - cevans - fix security problem with O_TRUNC and append only files
11
 */
12
 
13
/*
14
 * uClinux revisions for NO_MM
15
 * Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>,
16
 *                     The Silver Hammer Group, Ltd.
17
 */
18
 
19
#include <asm/segment.h>
20
 
21
#include <linux/errno.h>
22
#include <linux/sched.h>
23
#include <linux/kernel.h>
24
#include <linux/string.h>
25
#include <linux/fcntl.h>
26
#include <linux/stat.h>
27
#include <linux/mm.h>
28
 
29
#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
30
 
31
/*
32
 * How long a filename can we get from user space?
33
 *  -EFAULT if invalid area
34
 *  0 if ok (ENAMETOOLONG before EFAULT)
35
 *  >0 EFAULT after xx bytes
36
 */
37
static inline int get_max_filename(unsigned long address)
38
{
39
#ifndef NO_MM
40
        struct vm_area_struct * vma;
41
#endif /* !NO_MM */
42
 
43
        if (get_fs() == KERNEL_DS)
44
                return 0;
45
#ifndef NO_MM
46
        vma = find_vma(current->mm, address);
47
        if (!vma || vma->vm_start > address || !(vma->vm_flags & VM_READ))
48
                return -EFAULT;
49
        address = vma->vm_end - address;
50
        if (address > PAGE_SIZE)
51
                return 0;
52
        if (vma->vm_next && vma->vm_next->vm_start == vma->vm_end &&
53
           (vma->vm_next->vm_flags & VM_READ))
54
                return 0;
55
        return address;
56
#endif /* !NO_MM */
57
        return PAGE_SIZE;
58
}
59
 
60
/*
61
 * In order to reduce some races, while at the same time doing additional
62
 * checking and hopefully speeding things up, we copy filenames to the
63
 * kernel data space before using them..
64
 *
65
 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
66
 */
67
int getname(const char * filename, char **result)
68
{
69
        int i, error;
70
        unsigned long page;
71
        char * tmp, c;
72
 
73
        i = get_max_filename((unsigned long) filename);
74
        if (i < 0)
75
                return i;
76
        error = -EFAULT;
77
        if (!i) {
78
                error = -ENAMETOOLONG;
79
                i = PAGE_SIZE;
80
        }
81
        c = get_user(filename++);
82
        if (!c)
83
                return -ENOENT;
84
        if(!(page = __get_free_page(GFP_KERNEL)))
85
                return -ENOMEM;
86
        *result = tmp = (char *) page;
87
        while (--i) {
88
                *(tmp++) = c;
89
                c = get_user(filename++);
90
                if (!c) {
91
                        *tmp = '\0';
92
                        return 0;
93
                }
94
        }
95
        free_page(page);
96
        return error;
97
}
98
 
99
void putname(char * name)
100
{
101
        free_page((unsigned long) name);
102
}
103
 
104
/*
105
 *      permission()
106
 *
107
 * is used to check for read/write/execute permissions on a file.
108
 * We use "fsuid" for this, letting us set arbitrary permissions
109
 * for filesystem access without changing the "normal" uids which
110
 * are used for other things..
111
 */
112
int permission(struct inode * inode,int mask)
113
{
114
        int mode = inode->i_mode;
115
 
116
        if (inode->i_op && inode->i_op->permission)
117
                return inode->i_op->permission(inode, mask);
118
        else if ((mask & S_IWOTH) && IS_RDONLY(inode) &&
119
                 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
120
                return -EROFS; /* Nobody gets write access to a read-only fs */
121
        else if ((mask & S_IWOTH) && IS_IMMUTABLE(inode)) {
122
                return -EACCES; /* Nobody gets write access to an immutable file */
123
        }
124
        else if (current->fsuid == inode->i_uid)
125
                mode >>= 6;
126
        else if (in_group_p(inode->i_gid))
127
                mode >>= 3;
128
        if (((mode & mask & 0007) == mask) || fsuser())
129
                return 0;
130
        return -EACCES;
131
}
132
 
133
/*
134
 * get_write_access() gets write permission for a file.
135
 * put_write_access() releases this write permission.
136
 * This is used for regular files.
137
 * We cannot support write (and maybe mmap read-write shared) accesses and
138
 * MAP_DENYWRITE mmappings simultaneously.
139
 */
140
int get_write_access(struct inode * inode)
141
{
142
#ifndef NO_MM
143
        struct task_struct * p;
144
 
145
        if ((inode->i_count > 1) && S_ISREG(inode->i_mode)) /* shortcut */
146
                for_each_task(p) {
147
                        struct vm_area_struct * mpnt;
148
                        if (!p->mm)
149
                                continue;
150
                        for(mpnt = p->mm->mmap; mpnt; mpnt = mpnt->vm_next) {
151
                                if (inode != mpnt->vm_inode)
152
                                        continue;
153
                                if (mpnt->vm_flags & VM_DENYWRITE)
154
                                        return -ETXTBSY;
155
                        }
156
                }
157
#endif /* !NO_MM */
158
        inode->i_writecount++;
159
        return 0;
160
}
161
 
162
void put_write_access(struct inode * inode)
163
{
164
        inode->i_writecount--;
165
}
166
 
167
/*
168
 * lookup() looks up one part of a pathname, using the fs-dependent
169
 * routines (currently minix_lookup) for it. It also checks for
170
 * fathers (pseudo-roots, mount-points)
171
 */
172
int lookup(struct inode * dir,const char * name, int len,
173
           struct inode ** result)
174
{
175
        struct super_block * sb;
176
        int perm;
177
 
178
        *result = NULL;
179
        if (!dir)
180
                return -ENOENT;
181
/* check permissions before traversing mount-points */
182
        perm = permission(dir,MAY_EXEC);
183
        if (len==2 && name[0] == '.' && name[1] == '.') {
184
                if (dir == current->fs->root) {
185
                        *result = dir;
186
                        return 0;
187
                } else if ((sb = dir->i_sb) && (dir == sb->s_mounted)) {
188
                        iput(dir);
189
                        dir = sb->s_covered;
190
                        if (!dir)
191
                                return -ENOENT;
192
                        dir->i_count++;
193
                }
194
        }
195
        if (!dir->i_op || !dir->i_op->lookup) {
196
                iput(dir);
197
                return -ENOTDIR;
198
        }
199
        if (perm != 0) {
200
                iput(dir);
201
                return perm;
202
        }
203
        if (!len) {
204
                *result = dir;
205
                return 0;
206
        }
207
        return dir->i_op->lookup(dir, name, len, result);
208
}
209
 
210
int follow_link(struct inode * dir, struct inode * inode,
211
        int flag, int mode, struct inode ** res_inode)
212
{
213
        if (!dir || !inode) {
214
                iput(dir);
215
                iput(inode);
216
                *res_inode = NULL;
217
                return -ENOENT;
218
        }
219
        if (!inode->i_op || !inode->i_op->follow_link) {
220
                iput(dir);
221
                *res_inode = inode;
222
                return 0;
223
        }
224
        return inode->i_op->follow_link(dir,inode,flag,mode,res_inode);
225
}
226
 
227
/*
228
 *      dir_namei()
229
 *
230
 * dir_namei() returns the inode of the directory of the
231
 * specified name, and the name within that directory.
232
 */
233
static int dir_namei(const char *pathname, int *namelen, const char **name,
234
                     struct inode * base, struct inode **res_inode)
235
{
236
        char c;
237
        const char * thisname;
238
        int len,error;
239
        struct inode * inode;
240
 
241
        *res_inode = NULL;
242
        if (!base) {
243
                base = current->fs->pwd;
244
                base->i_count++;
245
        }
246
        if ((c = *pathname) == '/') {
247
                iput(base);
248
                base = current->fs->root;
249
                pathname++;
250
                base->i_count++;
251
        }
252
        while (1) {
253
                thisname = pathname;
254
                for(len=0;(c = *(pathname++))&&(c != '/');len++)
255
                        /* nothing */ ;
256
                if (!c)
257
                        break;
258
                base->i_count++;
259
                error = lookup(base, thisname, len, &inode);
260
                if (error) {
261
                        iput(base);
262
                        return error;
263
                }
264
                error = follow_link(base,inode,0,0,&base);
265
                if (error)
266
                        return error;
267
        }
268
        if (!base->i_op || !base->i_op->lookup) {
269
                iput(base);
270
                return -ENOTDIR;
271
        }
272
        *name = thisname;
273
        *namelen = len;
274
        *res_inode = base;
275
        return 0;
276
}
277
 
278
static int _namei(const char * pathname, struct inode * base,
279
                  int follow_links, struct inode ** res_inode)
280
{
281
        const char *basename;
282
        int namelen,error;
283
        struct inode * inode;
284
 
285
        *res_inode = NULL;
286
        error = dir_namei(pathname, &namelen, &basename, base, &base);
287
        if (error)
288
                return error;
289
        base->i_count++;        /* lookup uses up base */
290
        error = lookup(base, basename, namelen, &inode);
291
        if (error) {
292
                iput(base);
293
                return error;
294
        }
295
        if (follow_links) {
296
                error = follow_link(base, inode, 0, 0, &inode);
297
                if (error)
298
                        return error;
299
        } else
300
                iput(base);
301
        if ((inode->i_flags & S_BAD_INODE) != 0) {
302
                iput(inode);
303
                return -EIO;
304
        }
305
        *res_inode = inode;
306
        return 0;
307
}
308
 
309
int lnamei(const char *pathname, struct inode **res_inode)
310
{
311
        int error;
312
        char * tmp;
313
 
314
        error = getname(pathname, &tmp);
315
        if (!error) {
316
                error = _namei(tmp, NULL, 0, res_inode);
317
                putname(tmp);
318
        }
319
        return error;
320
}
321
 
322
/*
323
 *      namei()
324
 *
325
 * is used by most simple commands to get the inode of a specified name.
326
 * Open, link etc use their own routines, but this is enough for things
327
 * like 'chmod' etc.
328
 */
329
int namei(const char *pathname, struct inode **res_inode)
330
{
331
        int error;
332
        char * tmp;
333
 
334
        error = getname(pathname, &tmp);
335
        if (!error) {
336
                error = _namei(tmp, NULL, 1, res_inode);
337
                putname(tmp);
338
        }
339
        return error;
340
}
341
 
342
/*
343
 *      open_namei()
344
 *
345
 * namei for open - this is in fact almost the whole open-routine.
346
 *
347
 * Note that the low bits of "flag" aren't the same as in the open
348
 * system call - they are 00 - no permissions needed
349
 *                        01 - read permission needed
350
 *                        10 - write permission needed
351
 *                        11 - read/write permissions needed
352
 * which is a lot more logical, and also allows the "no perm" needed
353
 * for symlinks (where the permissions are checked later).
354
 */
355
int open_namei(const char * pathname, int flag, int mode,
356
               struct inode ** res_inode, struct inode * base)
357
{
358
        const char * basename;
359
        int namelen,error;
360
        struct inode * dir, *inode;
361
 
362
        mode &= S_IALLUGO & ~current->fs->umask;
363
        mode |= S_IFREG;
364
        error = dir_namei(pathname, &namelen, &basename, base, &dir);
365
        if (error)
366
                return error;
367
        if (!namelen) {                 /* special case: '/usr/' etc */
368
                if (flag & 2) {
369
                        iput(dir);
370
                        return -EISDIR;
371
                }
372
                /* thanks to Paul Pluzhnikov for noticing this was missing.. */
373
                if ((error = permission(dir,ACC_MODE(flag))) != 0) {
374
                        iput(dir);
375
                        return error;
376
                }
377
                *res_inode=dir;
378
                return 0;
379
        }
380
        dir->i_count++;         /* lookup eats the dir */
381
        if (flag & O_CREAT) {
382
                down(&dir->i_sem);
383
                error = lookup(dir, basename, namelen, &inode);
384
                if (!error) {
385
                        if (flag & O_EXCL) {
386
                                iput(inode);
387
                                error = -EEXIST;
388
                        }
389
                } else if (IS_RDONLY(dir)) {
390
                        error = -EROFS;
391
                }
392
                else if (!dir->i_op || !dir->i_op->create) {
393
                        error = -EACCES;
394
                }
395
                else if ((error = permission(dir,MAY_WRITE | MAY_EXEC)) != 0)
396
                        ;       /* error is already set! */
397
                else {
398
                        dir->i_count++;         /* create eats the dir */
399
                        if (dir->i_sb && dir->i_sb->dq_op)
400
                                dir->i_sb->dq_op->initialize(dir, -1);
401
                        error = dir->i_op->create(dir, basename, namelen, mode, res_inode);
402
                        up(&dir->i_sem);
403
                        iput(dir);
404
                        return error;
405
                }
406
                up(&dir->i_sem);
407
        } else
408
                error = lookup(dir, basename, namelen, &inode);
409
        if (error) {
410
                iput(dir);
411
                return error;
412
        }
413
        error = follow_link(dir,inode,flag,mode,&inode);
414
        if (error) {
415
                return error;
416
        }
417
        if (S_ISDIR(inode->i_mode) && (flag & 2)) {
418
                iput(inode);
419
                return -EISDIR;
420
        }
421
        if ((error = permission(inode,ACC_MODE(flag))) != 0) {
422
                iput(inode);
423
                return error;
424
        }
425
        if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
426
                /*
427
                 * 2-Feb-1995 Bruce Perens <Bruce@Pixar.com>
428
                 * Allow opens of Unix domain sockets and FIFOs for write on
429
                 * read-only filesystems. Their data does not live on the disk.
430
                 *
431
                 * If there was something like IS_NODEV(inode) for
432
                 * pipes and/or sockets I'd check it here.
433
                 */
434
                flag &= ~O_TRUNC;
435
        }
436
        else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
437
                if (IS_NODEV(inode)) {
438
                        iput(inode);
439
                        return -EACCES;
440
                }
441
                flag &= ~O_TRUNC;
442
        } else {
443
                if (IS_RDONLY(inode) && (flag & 2)) {
444
                        iput(inode);
445
                        return -EROFS;
446
                }
447
        }
448
        /*
449
         * An append-only file must be opened in append mode for writing
450
         * Additionally, we must disallow O_TRUNC -- cevans
451
         */
452
        if (IS_APPEND(inode) && (((flag & FMODE_WRITE) && !(flag & O_APPEND)) || (flag & O_TRUNC))) {
453
                iput(inode);
454
                return -EPERM;
455
        }
456
        if (flag & O_TRUNC) {
457
                if ((error = get_write_access(inode))) {
458
                        iput(inode);
459
                        return error;
460
                }
461
                /*
462
                 * Refuse to truncate files with mandatory locks held on them
463
                 */
464
                error = locks_verify_locked(inode);
465
                if (error) {
466
                        iput(inode);
467
                        return error;
468
                }
469
                if (inode->i_sb && inode->i_sb->dq_op)
470
                        inode->i_sb->dq_op->initialize(inode, -1);
471
 
472
                error = do_truncate(inode, 0);
473
                put_write_access(inode);
474
                if (error) {
475
                        iput(inode);
476
                        return error;
477
                }
478
        } else
479
                if (flag & FMODE_WRITE)
480
                        if (inode->i_sb && inode->i_sb->dq_op)
481
                                inode->i_sb->dq_op->initialize(inode, -1);
482
        *res_inode = inode;
483
        return 0;
484
}
485
 
486
int do_mknod(const char * filename, int mode, dev_t dev)
487
{
488
        const char * basename;
489
        int namelen, error;
490
        struct inode * dir;
491
 
492
        mode &= ~current->fs->umask;
493
        error = dir_namei(filename, &namelen, &basename, NULL, &dir);
494
        if (error)
495
                return error;
496
        if (!namelen) {
497
                iput(dir);
498
                return -ENOENT;
499
        }
500
        if (IS_RDONLY(dir)) {
501
                iput(dir);
502
                return -EROFS;
503
        }
504
        if ((error = permission(dir,MAY_WRITE | MAY_EXEC)) != 0) {
505
                iput(dir);
506
                return error;
507
        }
508
        if (!dir->i_op || !dir->i_op->mknod) {
509
                iput(dir);
510
                return -EPERM;
511
        }
512
        dir->i_count++;
513
        if (dir->i_sb && dir->i_sb->dq_op)
514
                dir->i_sb->dq_op->initialize(dir, -1);
515
        down(&dir->i_sem);
516
        error = dir->i_op->mknod(dir,basename,namelen,mode,dev);
517
        up(&dir->i_sem);
518
        iput(dir);
519
        return error;
520
}
521
 
522
asmlinkage int sys_mknod(const char * filename, int mode, dev_t dev)
523
{
524
        int error;
525
        char * tmp;
526
 
527
        if (S_ISDIR(mode) || (!S_ISFIFO(mode) && !fsuser()))
528
                return -EPERM;
529
        switch (mode & S_IFMT) {
530
        case 0:
531
                mode |= S_IFREG;
532
                break;
533
        case S_IFREG: case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
534
                break;
535
        default:
536
                return -EINVAL;
537
        }
538
        error = getname(filename,&tmp);
539
        if (!error) {
540
                error = do_mknod(tmp,mode,dev);
541
                putname(tmp);
542
        }
543
        return error;
544
}
545
 
546
/*
547
 * Some operations need to remove trailing slashes for POSIX.1
548
 * conformance. For rename we also need to change the behaviour
549
 * depending on whether we had a trailing slash or not.. (we
550
 * cannot rename normal files with trailing slashes, only dirs)
551
 *
552
 * "dummy" is used to make sure we don't do "/" -> "".
553
 */
554
static int remove_trailing_slashes(char * name)
555
{
556
        int result;
557
        char dummy[1];
558
        char *remove = dummy+1;
559
 
560
        for (;;) {
561
                char c = *name;
562
                name++;
563
                if (!c)
564
                        break;
565
                if (c != '/') {
566
                        remove = NULL;
567
                        continue;
568
                }
569
                if (remove)
570
                        continue;
571
                remove = name;
572
        }
573
 
574
        result = 0;
575
        if (remove) {
576
                remove[-1] = 0;
577
                result = 1;
578
        }
579
 
580
        return result;
581
}
582
 
583
static int do_mkdir(const char * pathname, int mode)
584
{
585
        const char * basename;
586
        int namelen, error;
587
        struct inode * dir;
588
 
589
        error = dir_namei(pathname, &namelen, &basename, NULL, &dir);
590
        if (error)
591
                return error;
592
        if (!namelen) {
593
                iput(dir);
594
                return -ENOENT;
595
        }
596
        if (IS_RDONLY(dir)) {
597
                iput(dir);
598
                return -EROFS;
599
        }
600
        if ((error = permission(dir,MAY_WRITE | MAY_EXEC)) != 0) {
601
                iput(dir);
602
                return error;
603
        }
604
        if (!dir->i_op || !dir->i_op->mkdir) {
605
                iput(dir);
606
                return -EPERM;
607
        }
608
        dir->i_count++;
609
        if (dir->i_sb && dir->i_sb->dq_op)
610
                dir->i_sb->dq_op->initialize(dir, -1);
611
        down(&dir->i_sem);
612
        error = dir->i_op->mkdir(dir, basename, namelen, mode & 01777 & ~current->fs->umask);
613
        up(&dir->i_sem);
614
        iput(dir);
615
        return error;
616
}
617
 
618
asmlinkage int sys_mkdir(const char * pathname, int mode)
619
{
620
        int error;
621
        char * tmp;
622
 
623
        error = getname(pathname,&tmp);
624
        if (!error) {
625
                remove_trailing_slashes(tmp);
626
                error = do_mkdir(tmp,mode);
627
                putname(tmp);
628
        }
629
        return error;
630
}
631
 
632
static int do_rmdir(const char * name)
633
{
634
        const char * basename;
635
        int namelen, error;
636
        struct inode * dir;
637
 
638
        error = dir_namei(name, &namelen, &basename, NULL, &dir);
639
        if (error)
640
                return error;
641
        if (!namelen) {
642
                iput(dir);
643
                return -ENOENT;
644
        }
645
        if (IS_RDONLY(dir)) {
646
                iput(dir);
647
                return -EROFS;
648
        }
649
        if ((error = permission(dir,MAY_WRITE | MAY_EXEC)) != 0) {
650
                iput(dir);
651
                return error;
652
        }
653
        /*
654
         * A subdirectory cannot be removed from an append-only directory
655
         */
656
        if (IS_APPEND(dir)) {
657
                iput(dir);
658
                return -EPERM;
659
        }
660
        if (!dir->i_op || !dir->i_op->rmdir) {
661
                iput(dir);
662
                return -EPERM;
663
        }
664
        if (dir->i_sb && dir->i_sb->dq_op)
665
                dir->i_sb->dq_op->initialize(dir, -1);
666
        down(&dir->i_sem);
667
        error = dir->i_op->rmdir(dir,basename,namelen);
668
        up(&dir->i_sem);
669
        return error;
670
}
671
 
672
asmlinkage int sys_rmdir(const char * pathname)
673
{
674
        int error;
675
        char * tmp;
676
 
677
        error = getname(pathname,&tmp);
678
        if (!error) {
679
                remove_trailing_slashes(tmp);
680
                error = do_rmdir(tmp);
681
                putname(tmp);
682
        }
683
        return error;
684
}
685
 
686
static int do_unlink(const char * name)
687
{
688
        const char * basename;
689
        int namelen, error;
690
        struct inode * dir;
691
 
692
        error = dir_namei(name, &namelen, &basename, NULL, &dir);
693
        if (error)
694
                return error;
695
        if (!namelen) {
696
                iput(dir);
697
                return -EPERM;
698
        }
699
        if (IS_RDONLY(dir)) {
700
                iput(dir);
701
                return -EROFS;
702
        }
703
        if ((error = permission(dir,MAY_WRITE | MAY_EXEC)) != 0) {
704
                iput(dir);
705
                return error;
706
        }
707
        /*
708
         * A file cannot be removed from an append-only directory
709
         */
710
        if (IS_APPEND(dir)) {
711
                iput(dir);
712
                return -EPERM;
713
        }
714
        if (!dir->i_op || !dir->i_op->unlink) {
715
                iput(dir);
716
                return -EPERM;
717
        }
718
        if (dir->i_sb && dir->i_sb->dq_op)
719
                dir->i_sb->dq_op->initialize(dir, -1);
720
        down(&dir->i_sem);
721
        error = dir->i_op->unlink(dir,basename,namelen);
722
        up(&dir->i_sem);
723
        return error;
724
}
725
 
726
asmlinkage int sys_unlink(const char * pathname)
727
{
728
        int error;
729
        char * tmp;
730
 
731
        error = getname(pathname,&tmp);
732
        if (!error) {
733
                error = do_unlink(tmp);
734
                putname(tmp);
735
        }
736
        return error;
737
}
738
 
739
static int do_symlink(const char * oldname, const char * newname)
740
{
741
        struct inode * dir;
742
        const char * basename;
743
        int namelen, error;
744
 
745
        error = dir_namei(newname, &namelen, &basename, NULL, &dir);
746
        if (error)
747
                return error;
748
        if (!namelen) {
749
                iput(dir);
750
                return -ENOENT;
751
        }
752
        if (IS_RDONLY(dir)) {
753
                iput(dir);
754
                return -EROFS;
755
        }
756
        if ((error = permission(dir,MAY_WRITE | MAY_EXEC)) != 0) {
757
                iput(dir);
758
                return error;
759
        }
760
        if (!dir->i_op || !dir->i_op->symlink) {
761
                iput(dir);
762
                return -EPERM;
763
        }
764
        dir->i_count++;
765
        if (dir->i_sb && dir->i_sb->dq_op)
766
                dir->i_sb->dq_op->initialize(dir, -1);
767
        down(&dir->i_sem);
768
        error = dir->i_op->symlink(dir,basename,namelen,oldname);
769
        up(&dir->i_sem);
770
        iput(dir);
771
        return error;
772
}
773
 
774
asmlinkage int sys_symlink(const char * oldname, const char * newname)
775
{
776
        int error;
777
        char * from, * to;
778
 
779
        error = getname(oldname,&from);
780
        if (!error) {
781
                error = getname(newname,&to);
782
                if (!error) {
783
                        error = do_symlink(from,to);
784
                        putname(to);
785
                }
786
                putname(from);
787
        }
788
        return error;
789
}
790
 
791
static int do_link(struct inode * oldinode, const char * newname)
792
{
793
        struct inode * dir;
794
        const char * basename;
795
        int namelen, error;
796
 
797
        error = dir_namei(newname, &namelen, &basename, NULL, &dir);
798
        if (error) {
799
                iput(oldinode);
800
                return error;
801
        }
802
        if (!namelen) {
803
                iput(oldinode);
804
                iput(dir);
805
                return -EPERM;
806
        }
807
        if (IS_RDONLY(dir)) {
808
                iput(oldinode);
809
                iput(dir);
810
                return -EROFS;
811
        }
812
        if (dir->i_dev != oldinode->i_dev) {
813
                iput(dir);
814
                iput(oldinode);
815
                return -EXDEV;
816
        }
817
        if ((error = permission(dir,MAY_WRITE | MAY_EXEC)) != 0) {
818
                iput(dir);
819
                iput(oldinode);
820
                return error;
821
        }
822
        /*
823
         * A link to an append-only or immutable file cannot be created
824
         */
825
        if (IS_APPEND(oldinode) || IS_IMMUTABLE(oldinode)) {
826
                iput(dir);
827
                iput(oldinode);
828
                return -EPERM;
829
        }
830
        if (!dir->i_op || !dir->i_op->link) {
831
                iput(dir);
832
                iput(oldinode);
833
                return -EPERM;
834
        }
835
        dir->i_count++;
836
        if (dir->i_sb && dir->i_sb->dq_op)
837
                dir->i_sb->dq_op->initialize(dir, -1);
838
        down(&dir->i_sem);
839
        error = dir->i_op->link(oldinode, dir, basename, namelen);
840
        up(&dir->i_sem);
841
        iput(dir);
842
        return error;
843
}
844
 
845
asmlinkage int sys_link(const char * oldname, const char * newname)
846
{
847
        int error;
848
        char * to;
849
        struct inode * oldinode;
850
 
851
        error = lnamei(oldname, &oldinode);
852
        if (error)
853
                return error;
854
        error = getname(newname,&to);
855
        if (error) {
856
                iput(oldinode);
857
                return error;
858
        }
859
        error = do_link(oldinode,to);
860
        putname(to);
861
        return error;
862
}
863
 
864
static int do_rename(const char * oldname, const char * newname, int must_be_dir)
865
{
866
        struct inode * old_dir, * new_dir;
867
        const char * old_base, * new_base;
868
        int old_len, new_len, error;
869
 
870
        error = dir_namei(oldname, &old_len, &old_base, NULL, &old_dir);
871
        if (error)
872
                return error;
873
        if ((error = permission(old_dir,MAY_WRITE | MAY_EXEC)) != 0) {
874
                iput(old_dir);
875
                return error;
876
        }
877
        if (!old_len || (old_base[0] == '.' &&
878
            (old_len == 1 || (old_base[1] == '.' &&
879
             old_len == 2)))) {
880
                iput(old_dir);
881
                return -EPERM;
882
        }
883
        error = dir_namei(newname, &new_len, &new_base, NULL, &new_dir);
884
        if (error) {
885
                iput(old_dir);
886
                return error;
887
        }
888
        if ((error = permission(new_dir,MAY_WRITE | MAY_EXEC)) != 0){
889
                iput(old_dir);
890
                iput(new_dir);
891
                return error;
892
        }
893
        if (!new_len || (new_base[0] == '.' &&
894
            (new_len == 1 || (new_base[1] == '.' &&
895
             new_len == 2)))) {
896
                iput(old_dir);
897
                iput(new_dir);
898
                return -EPERM;
899
        }
900
        if (new_dir->i_dev != old_dir->i_dev) {
901
                iput(old_dir);
902
                iput(new_dir);
903
                return -EXDEV;
904
        }
905
        if (IS_RDONLY(new_dir) || IS_RDONLY(old_dir)) {
906
                iput(old_dir);
907
                iput(new_dir);
908
                return -EROFS;
909
        }
910
        /*
911
         * A file cannot be removed from an append-only directory
912
         */
913
        if (IS_APPEND(old_dir)) {
914
                iput(old_dir);
915
                iput(new_dir);
916
                return -EPERM;
917
        }
918
        if (!old_dir->i_op || !old_dir->i_op->rename) {
919
                iput(old_dir);
920
                iput(new_dir);
921
                return -EPERM;
922
        }
923
        new_dir->i_count++;
924
        if (new_dir->i_sb && new_dir->i_sb->dq_op)
925
                new_dir->i_sb->dq_op->initialize(new_dir, -1);
926
        down(&new_dir->i_sem);
927
        error = old_dir->i_op->rename(old_dir, old_base, old_len,
928
                new_dir, new_base, new_len, must_be_dir);
929
        up(&new_dir->i_sem);
930
        iput(new_dir);
931
        return error;
932
}
933
 
934
asmlinkage int sys_rename(const char * oldname, const char * newname)
935
{
936
        int error;
937
        char * from, * to;
938
 
939
        error = getname(oldname,&from);
940
        if (!error) {
941
                error = getname(newname,&to);
942
                if (!error) {
943
                        error = do_rename(from,to,
944
                                remove_trailing_slashes(from) |
945
                                remove_trailing_slashes(to));
946
                        putname(to);
947
                }
948
                putname(from);
949
        }
950
        return error;
951
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.