1 /*
2  *  linux/fs/ncpfs/symlink.c
3  *
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.
9  *
10  *  from linux/fs/ext2/symlink.c
11  *
12  *  Copyright (C) 1998-99, Frank A. Vorstenbosch
13  *
14  *  ncpfs symlink handling code
15  *  NLS support (c) 1999 Petr Vandrovec
16  *
17  */
18 
19 #include <linux/config.h>
20 
21 #ifdef CONFIG_NCPFS_EXTRAS
22 
23 #include <asm/uaccess.h>
24 #include <asm/segment.h>
25 
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"
33 
34 
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. */
37 
38 #define NCP_SYMLINK_MAGIC0	le32_to_cpu(0x6c6d7973)     /* "symlnk->" */
39 #define NCP_SYMLINK_MAGIC1	le32_to_cpu(0x3e2d6b6e)
40 
41 int ncp_create_new(struct inode *dir, struct dentry *dentry,
42                           int mode,int attributes);
43 
44 /* ----- read a symbolic link ------------------------------------------ */
45 
ncp_symlink_readpage(struct file * file,struct page * page)46 static int ncp_symlink_readpage(struct file *file, struct page *page)
47 {
48 	struct inode *inode = page->mapping->host;
49 	int error, length, len, cnt;
50 	char *link;
51 	char *buf = kmap(page);
52 
53 	error = -ENOMEM;
54 	for (cnt = 0; (link=(char *)kmalloc(NCP_MAX_SYMLINK_SIZE, GFP_NFS))==NULL; cnt++) {
55 		if (cnt > 10)
56 			goto fail;
57 		schedule();
58 	}
59 
60 	if (ncp_make_open(inode,O_RDONLY))
61 		goto failEIO;
62 
63 	error=ncp_read_kernel(NCP_SERVER(inode),NCP_FINFO(inode)->file_handle,
64                          0,NCP_MAX_SYMLINK_SIZE,link,&length);
65 
66 	ncp_inode_close(inode);
67 	/* Close file handle if no other users... */
68 	ncp_make_closed(inode);
69 	if (error)
70 		goto failEIO;
71 
72 	if (length<NCP_MIN_SYMLINK_SIZE ||
73 	    ((__u32 *)link)[0]!=NCP_SYMLINK_MAGIC0 ||
74 	    ((__u32 *)link)[1]!=NCP_SYMLINK_MAGIC1)
75 	    	goto failEIO;
76 
77 	len = NCP_MAX_SYMLINK_SIZE;
78 	error = ncp_vol2io(NCP_SERVER(inode), buf, &len, link+8, length-8, 0);
79 	kfree(link);
80 	if (error)
81 		goto fail;
82 	SetPageUptodate(page);
83 	kunmap(page);
84 	UnlockPage(page);
85 	return 0;
86 
87 failEIO:
88 	error = -EIO;
89 	kfree(link);
90 fail:
91 	SetPageError(page);
92 	kunmap(page);
93 	UnlockPage(page);
94 	return error;
95 }
96 
97 /*
98  * symlinks can't do much...
99  */
100 struct address_space_operations ncp_symlink_aops = {
101 	readpage:	ncp_symlink_readpage,
102 };
103 
104 /* ----- create a new symbolic link -------------------------------------- */
105 
ncp_symlink(struct inode * dir,struct dentry * dentry,const char * symname)106 int ncp_symlink(struct inode *dir, struct dentry *dentry, const char *symname) {
107 	struct inode *inode;
108 	char *link;
109 	int length, err, i;
110 
111 #ifdef DEBUG
112 	PRINTK("ncp_symlink(dir=%p,dentry=%p,symname=%s)\n",dir,dentry,symname);
113 #endif
114 
115 	if (!(NCP_SERVER(dir)->m.flags & NCP_MOUNT_SYMLINKS))
116 		return -EPERM;	/* EPERM is returned by VFS if symlink procedure does not exist */
117 
118 	if ((length=strlen(symname))>NCP_MAX_SYMLINK_SIZE-8)
119 		return -EINVAL;
120 
121 	if ((link=(char *)kmalloc(length+9,GFP_NFS))==NULL)
122 		return -ENOMEM;
123 
124 	err = -EIO;
125 	if (ncp_create_new(dir,dentry,0,aSHARED|aHIDDEN))
126 		goto failfree;
127 
128 	inode=dentry->d_inode;
129 
130 	if (ncp_make_open(inode, O_WRONLY))
131 		goto failfree;
132 
133 	((__u32 *)link)[0]=NCP_SYMLINK_MAGIC0;
134 	((__u32 *)link)[1]=NCP_SYMLINK_MAGIC1;
135 
136 	/* map to/from server charset, do not touch upper/lower case as
137 	   symlink can point out of ncp filesystem */
138 	length += 1;
139 	err = ncp_io2vol(NCP_SERVER(inode),link+8,&length,symname,length-1,0);
140 	if (err)
141 		goto fail;
142 
143 	if(ncp_write_kernel(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
144 	    		    0, length+8, link, &i) || i!=length+8) {
145 		err = -EIO;
146 		goto fail;
147 	}
148 
149 	ncp_inode_close(inode);
150 	ncp_make_closed(inode);
151 	kfree(link);
152 	return 0;
153 
154 fail:
155 	ncp_inode_close(inode);
156 	ncp_make_closed(inode);
157 failfree:
158 	kfree(link);
159 	return err;
160 }
161 #endif
162 
163 /* ----- EOF ----- */
164