Import 2.3.30pre7
[davej-history.git] / fs / ncpfs / symlink.c
blob5d6954df9b0a0928d862157a1a1d2066eef22105
1 /*
2 * linux/fs/ncpfs/symlink.c
4 * Code for allowing symbolic links on NCPFS (i.e. NetWare)
5 * Symbolic links are not supported on native NetWare, so we use an
6 * infrequently-used flag (Sh) and store a two-word magic header in
7 * the file to make sure we don't accidentally use a non-link file
8 * as a link.
10 * from linux/fs/ext2/symlink.c
12 * Copyright (C) 1998-99, Frank A. Vorstenbosch
14 * ncpfs symlink handling code
15 * NLS support (c) 1999 Petr Vandrovec
19 #include <linux/config.h>
21 #ifdef CONFIG_NCPFS_EXTRAS
23 #include <asm/uaccess.h>
24 #include <asm/segment.h>
26 #include <linux/errno.h>
27 #include <linux/fs.h>
28 #include <linux/ncp_fs.h>
29 #include <linux/sched.h>
30 #include <linux/mm.h>
31 #include <linux/stat.h>
32 #include "ncplib_kernel.h"
35 /* these magic numbers must appear in the symlink file -- this makes it a bit
36 more resilient against the magic attributes being set on random files. */
38 #define NCP_SYMLINK_MAGIC0 le32_to_cpu(0x6c6d7973) /* "symlnk->" */
39 #define NCP_SYMLINK_MAGIC1 le32_to_cpu(0x3e2d6b6e)
41 static int ncp_readlink(struct dentry *, char *, int);
42 static struct dentry *ncp_follow_link(struct dentry *, struct dentry *, unsigned int);
43 int ncp_create_new(struct inode *dir, struct dentry *dentry,
44 int mode,int attributes);
47 * symlinks can't do much...
49 struct inode_operations ncp_symlink_inode_operations={
50 NULL, /* no file-operations */
51 NULL, /* create */
52 NULL, /* lookup */
53 NULL, /* link */
54 NULL, /* unlink */
55 NULL, /* symlink */
56 NULL, /* mkdir */
57 NULL, /* rmdir */
58 NULL, /* mknod */
59 NULL, /* rename */
60 ncp_readlink, /* readlink */
61 ncp_follow_link, /* follow_link */
62 NULL, /* get_block */
63 NULL, /* readpage */
64 NULL, /* writepage */
65 NULL, /* truncate */
66 NULL, /* permission */
67 NULL /* revalidate */
70 /* ----- follow a symbolic link ------------------------------------------ */
72 static struct dentry *ncp_follow_link(struct dentry *dentry,
73 struct dentry *base,
74 unsigned int follow)
76 struct inode *inode=dentry->d_inode;
77 int error, length, cnt;
78 char *link;
80 #ifdef DEBUG
81 PRINTK("ncp_follow_link(dentry=%p,base=%p,follow=%u)\n",dentry,base,follow);
82 #endif
84 if(!S_ISLNK(inode->i_mode)) {
85 dput(base);
86 return ERR_PTR(-EINVAL);
89 if(ncp_make_open(inode,O_RDONLY)) {
90 dput(base);
91 return ERR_PTR(-EIO);
94 for (cnt = 0; (link=(char *)kmalloc(NCP_MAX_SYMLINK_SIZE+1, GFP_NFS))==NULL; cnt++) {
95 if (cnt > 10) {
96 dput(base);
97 return ERR_PTR(-EAGAIN); /* -ENOMEM? */
99 schedule();
102 error=ncp_read_kernel(NCP_SERVER(inode),NCP_FINFO(inode)->file_handle,
103 0,NCP_MAX_SYMLINK_SIZE,link,&length);
105 if (error!=0 || length<NCP_MIN_SYMLINK_SIZE ||
106 ((__u32 *)link)[0]!=NCP_SYMLINK_MAGIC0 || ((__u32 *)link)[1]!=NCP_SYMLINK_MAGIC1) {
107 dput(base);
108 kfree(link);
109 return ERR_PTR(-EIO);
112 link[length]=0;
114 vol2io(NCP_SERVER(inode), link+8, 0);
116 /* UPDATE_ATIME(inode); */
117 base=lookup_dentry(link+8, base, follow);
118 kfree(link);
120 return base;
123 /* ----- read symbolic link ---------------------------------------------- */
125 static int ncp_readlink(struct dentry * dentry, char * buffer, int buflen)
127 struct inode *inode=dentry->d_inode;
128 char *link;
129 int length,error;
131 #ifdef DEBUG
132 PRINTK("ncp_readlink(dentry=%p,buffer=%p,buflen=%d)\n",dentry,buffer,buflen);
133 #endif
135 if(!S_ISLNK(inode->i_mode))
136 return -EINVAL;
138 if(ncp_make_open(inode,O_RDONLY))
139 return -EIO;
141 if((link=(char *)kmalloc(NCP_MAX_SYMLINK_SIZE+1,GFP_NFS))==NULL)
142 return -ENOMEM;
144 error = ncp_read_kernel(NCP_SERVER(inode),NCP_FINFO(inode)->file_handle,
145 0,NCP_MAX_SYMLINK_SIZE,link,&length);
147 if (error!=0 || length < NCP_MIN_SYMLINK_SIZE || buflen < (length-8) ||
148 ((__u32 *)link)[0]!=NCP_SYMLINK_MAGIC0 ||((__u32 *)link)[1]!=NCP_SYMLINK_MAGIC1) {
149 error = -EIO;
150 goto out;
153 link[length] = 0;
155 vol2io(NCP_SERVER(inode), link+8, 0);
157 error = length - 8;
158 if(copy_to_user(buffer, link+8, error))
159 error = -EFAULT;
161 out:;
162 kfree(link);
163 return error;
166 /* ----- create a new symbolic link -------------------------------------- */
168 int ncp_symlink(struct inode *dir, struct dentry *dentry, const char *symname) {
169 int i,length;
170 struct inode *inode;
171 char *link;
173 #ifdef DEBUG
174 PRINTK("ncp_symlink(dir=%p,dentry=%p,symname=%s)\n",dir,dentry,symname);
175 #endif
177 if (!(NCP_SERVER(dir)->m.flags & NCP_MOUNT_SYMLINKS))
178 return -EPERM; /* EPERM is returned by VFS if symlink procedure does not exist */
180 if ((length=strlen(symname))>NCP_MAX_SYMLINK_SIZE)
181 return -EINVAL;
183 if ((link=(char *)kmalloc(length+9,GFP_NFS))==NULL)
184 return -ENOMEM;
186 if (ncp_create_new(dir,dentry,0,aSHARED|aHIDDEN)) {
187 kfree(link);
188 return -EIO;
191 inode=dentry->d_inode;
193 ((__u32 *)link)[0]=NCP_SYMLINK_MAGIC0;
194 ((__u32 *)link)[1]=NCP_SYMLINK_MAGIC1;
195 memcpy(link+8, symname, length+1); /* including last zero for io2vol */
197 /* map to/from server charset, do not touch upper/lower case as
198 symlink can point out of ncp filesystem */
199 io2vol(NCP_SERVER(inode), link+8, 0);
201 if(ncp_write_kernel(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
202 0, length+8, link, &i) || i!=length+8) {
203 kfree(link);
204 return -EIO;
207 kfree(link);
208 return 0;
210 #endif
212 /* ----- EOF ----- */