* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-0.99 / fs / ext2 / symlink.c
bloba98ed66bbffbcb70350bb8f51e770b29ef720d09
1 /*
2 * linux/fs/ext2/symlink.c
4 * Copyright (C) 1992, 1993, 1994 Remy Card (card@masi.ibp.fr)
5 * Laboratoire MASI - Institut Blaise Pascal
6 * Universite Pierre et Marie Curie (Paris VI)
8 * from
10 * linux/fs/minix/symlink.c
12 * Copyright (C) 1991, 1992 Linus Torvalds
14 * ext2 symlink handling code
17 #include <asm/segment.h>
19 #include <linux/errno.h>
20 #include <linux/fs.h>
21 #include <linux/ext2_fs.h>
22 #include <linux/sched.h>
23 #include <linux/stat.h>
25 static int ext2_readlink (struct inode *, char *, int);
26 static int ext2_follow_link (struct inode *, struct inode *, int, int,
27 struct inode **);
30 * symlinks can't do much...
32 struct inode_operations ext2_symlink_inode_operations = {
33 NULL, /* no file-operations */
34 NULL, /* create */
35 NULL, /* lookup */
36 NULL, /* link */
37 NULL, /* unlink */
38 NULL, /* symlink */
39 NULL, /* mkdir */
40 NULL, /* rmdir */
41 NULL, /* mknod */
42 NULL, /* rename */
43 ext2_readlink, /* readlink */
44 ext2_follow_link, /* follow_link */
45 NULL, /* bmap */
46 NULL, /* truncate */
47 NULL /* permission */
50 static int ext2_follow_link(struct inode * dir, struct inode * inode,
51 int flag, int mode, struct inode ** res_inode)
53 int error;
54 struct buffer_head * bh = NULL;
55 char * link;
57 *res_inode = NULL;
58 if (!dir) {
59 dir = current->root;
60 dir->i_count++;
62 if (!inode) {
63 iput (dir);
64 return -ENOENT;
66 if (!S_ISLNK(inode->i_mode)) {
67 iput (dir);
68 *res_inode = inode;
69 return 0;
71 if (current->link_count > 5) {
72 iput (dir);
73 iput (inode);
74 return -ELOOP;
76 if (inode->i_blocks) {
77 if (!(bh = ext2_bread (inode, 0, 0, &error))) {
78 iput (dir);
79 iput (inode);
80 return -EIO;
82 link = bh->b_data;
83 } else
84 link = (char *) inode->u.ext2_i.i_data;
85 current->link_count++;
86 error = open_namei (link, flag, mode, res_inode, dir);
87 current->link_count--;
88 iput (inode);
89 if (bh)
90 brelse (bh);
91 return error;
94 static int ext2_readlink (struct inode * inode, char * buffer, int buflen)
96 struct buffer_head * bh = NULL;
97 char * link;
98 int i, err;
99 char c;
101 if (!S_ISLNK(inode->i_mode)) {
102 iput (inode);
103 return -EINVAL;
105 if (buflen > inode->i_sb->s_blocksize - 1)
106 buflen = inode->i_sb->s_blocksize - 1;
107 if (inode->i_blocks) {
108 bh = ext2_bread (inode, 0, 0, &err);
109 if (!bh) {
110 iput (inode);
111 return 0;
113 link = bh->b_data;
115 else
116 link = (char *) inode->u.ext2_i.i_data;
117 i = 0;
118 while (i < buflen && (c = link[i])) {
119 i++;
120 put_fs_byte (c, buffer++);
122 iput (inode);
123 if (bh)
124 brelse (bh);
125 return i;