ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / cifs / link.c
1 /*
2  *   fs/cifs/link.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2003
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
7  *   This library is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU Lesser General Public License as published
9  *   by the Free Software Foundation; either version 2.1 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This library is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
15  *   the GNU Lesser General Public License for more details.
16  *
17  *   You should have received a copy of the GNU Lesser General Public License
18  *   along with this library; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 #include <linux/fs.h>
22 #include <linux/stat.h>
23 #include "cifsfs.h"
24 #include "cifspdu.h"
25 #include "cifsglob.h"
26 #include "cifsproto.h"
27 #include "cifs_debug.h"
28 #include "cifs_fs_sb.h"
29
30 int
31 cifs_hardlink(struct dentry *old_file, struct inode *inode,
32               struct dentry *direntry)
33 {
34         int rc = -EACCES;
35         int xid;
36         char *fromName = NULL;
37         char *toName = NULL;
38         struct cifs_sb_info *cifs_sb_target;
39         struct cifsTconInfo *pTcon;
40         struct cifsInodeInfo *cifsInode;
41
42         xid = GetXid();
43
44         cifs_sb_target = CIFS_SB(inode->i_sb);
45         pTcon = cifs_sb_target->tcon;
46
47 /* No need to check for cross device links since server will do that - BB note DFS case in future though (when we may have to check) */
48
49         fromName = build_path_from_dentry(old_file);
50         toName = build_path_from_dentry(direntry);
51         if (cifs_sb_target->tcon->ses->capabilities & CAP_UNIX)
52                 rc = CIFSUnixCreateHardLink(xid, pTcon, fromName, toName,
53                                             cifs_sb_target->local_nls);
54         else {
55                 rc = CIFSCreateHardLink(xid, pTcon, fromName, toName,
56                                         cifs_sb_target->local_nls);
57                 if(rc == -EIO)
58                         rc = -EOPNOTSUPP;  
59         }
60
61 /* if (!rc)     */
62         {
63                 /*   renew_parental_timestamps(old_file);
64                    inode->i_nlink++;
65                    mark_inode_dirty(inode);
66                    d_instantiate(direntry, inode); */
67                 /* BB add call to either mark inode dirty or refresh its data and timestamp to current time */
68         }
69         d_drop(direntry);       /* force new lookup from server */
70         cifsInode = CIFS_I(old_file->d_inode);
71         cifsInode->time = 0;    /* will force revalidate to go get info when needed */
72
73         if (fromName)
74                 kfree(fromName);
75         if (toName)
76                 kfree(toName);
77         FreeXid(xid);
78         return rc;
79 }
80
81 int
82 cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
83 {
84         struct inode *inode = direntry->d_inode;
85         int rc = -EACCES;
86         int xid;
87         char *full_path = NULL;
88         char * target_path;
89         struct cifs_sb_info *cifs_sb;
90         struct cifsTconInfo *pTcon;
91
92         xid = GetXid();
93         full_path = build_path_from_dentry(direntry);
94         cFYI(1, ("Full path: %s inode = 0x%p", full_path, inode));
95         cifs_sb = CIFS_SB(inode->i_sb);
96         pTcon = cifs_sb->tcon;
97         target_path = kmalloc(PATH_MAX, GFP_KERNEL);
98         if(target_path == NULL) {
99                 if (full_path)
100                         kfree(full_path);
101                 FreeXid(xid);
102                 return -ENOMEM;
103         }
104         /* can not call the following line due to EFAULT in vfs_readlink which is presumably expecting a user space buffer */
105         /* length = cifs_readlink(direntry,target_path, sizeof(target_path) - 1);    */
106
107 /* BB add read reparse point symlink code and Unix extensions symlink code here BB */
108         if (pTcon->ses->capabilities & CAP_UNIX)
109                 rc = CIFSSMBUnixQuerySymLink(xid, pTcon, full_path,
110                                              target_path,
111                                              PATH_MAX-1,
112                                              cifs_sb->local_nls);
113         else {
114                 /* rc = CIFSSMBQueryReparseLinkInfo */
115                 /* BB Add code to Query ReparsePoint info */
116         }
117         /* BB Anything else to do to handle recursive links? */
118         /* BB Should we be using page symlink ops here? */
119
120         if (rc == 0) {
121
122 /* BB Add special case check for Samba DFS symlinks */
123
124                 target_path[PATH_MAX-1] = 0;
125                 rc = vfs_follow_link(nd, target_path);
126         }
127         /* else EACCESS */
128
129         if (target_path)
130                 kfree(target_path);
131         if (full_path)
132                 kfree(full_path);
133         FreeXid(xid);
134         return rc;
135 }
136
137 int
138 cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
139 {
140         int rc = -EOPNOTSUPP;
141         int xid;
142         struct cifs_sb_info *cifs_sb;
143         struct cifsTconInfo *pTcon;
144         char *full_path = NULL;
145         struct inode *newinode = NULL;
146
147         xid = GetXid();
148
149         cifs_sb = CIFS_SB(inode->i_sb);
150         pTcon = cifs_sb->tcon;
151
152         full_path = build_path_from_dentry(direntry);
153         cFYI(1, ("Full path: %s ", full_path));
154         cFYI(1, ("symname is %s", symname));
155
156         /* BB what if DFS and this volume is on different share? BB */
157         if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
158                 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
159                                            cifs_sb->local_nls);
160         /* else
161            rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,cifs_sb_target->local_nls); */
162
163         if (rc == 0) {
164                 if (pTcon->ses->capabilities & CAP_UNIX)
165                         rc = cifs_get_inode_info_unix(&newinode, full_path,
166                                                       inode->i_sb);
167                 else
168                         rc = cifs_get_inode_info(&newinode, full_path, NULL,
169                                                  inode->i_sb);
170
171                 if (rc != 0) {
172                         cFYI(1,
173                              ("Create symlink worked but get_inode_info failed with rc = %d ",
174                               rc));
175                 } else {
176                         direntry->d_op = &cifs_dentry_ops;
177                         d_instantiate(direntry, newinode);
178                 }
179         }
180
181         if (full_path)
182                 kfree(full_path);
183         FreeXid(xid);
184         return rc;
185 }
186
187 int
188 cifs_readlink(struct dentry *direntry, char *pBuffer, int buflen)
189 {
190         struct inode *inode = direntry->d_inode;
191         int rc = -EACCES;
192         int xid;
193         int oplock = FALSE;
194         struct cifs_sb_info *cifs_sb;
195         struct cifsTconInfo *pTcon;
196         char *full_path = NULL;
197         char *tmp_path =  NULL;
198         char * tmpbuffer;
199         unsigned char * referrals = NULL;
200         int num_referrals = 0;
201         int len;
202         __u16 fid;
203
204         xid = GetXid();
205         cifs_sb = CIFS_SB(inode->i_sb);
206         pTcon = cifs_sb->tcon;
207         full_path = build_path_from_dentry(direntry);
208         cFYI(1,
209              ("Full path: %s inode = 0x%p pBuffer = 0x%p buflen = %d",
210               full_path, inode, pBuffer, buflen));
211         if(buflen > PATH_MAX)
212                 len = PATH_MAX;
213         else
214                 len = buflen;
215         tmpbuffer = kmalloc(len,GFP_KERNEL);   
216         if(tmpbuffer == NULL) {
217                 if (full_path)
218                         kfree(full_path);
219                 FreeXid(xid);
220                 return -ENOMEM;
221         }
222
223 /* BB add read reparse point symlink code and Unix extensions symlink code here BB */
224         if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
225                 rc = CIFSSMBUnixQuerySymLink(xid, pTcon, full_path,
226                                 tmpbuffer,
227                                 len - 1,
228                                 cifs_sb->local_nls);
229         else {
230                 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, GENERIC_READ,
231                                 OPEN_REPARSE_POINT,&fid, &oplock, NULL, cifs_sb->local_nls);
232                 if(!rc) {
233                         rc = CIFSSMBQueryReparseLinkInfo(xid, pTcon, full_path,
234                                 tmpbuffer,
235                                 len - 1, 
236                                 fid,
237                                 cifs_sb->local_nls);
238                         if(CIFSSMBClose(xid, pTcon, fid)) {
239                                 cFYI(1,("Error closing junction point (open for ioctl)"));
240                         }
241                         if(rc == -EIO) {
242                                 /* Query if DFS Junction */
243                                 tmp_path =
244                                         kmalloc(MAX_TREE_SIZE + MAX_PATHCONF + 1,
245                                                 GFP_KERNEL);
246                                 if (tmp_path) {
247                                         strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
248                                         strncat(tmp_path, full_path, MAX_PATHCONF);
249                                         rc = get_dfs_path(xid, pTcon->ses, tmp_path,
250                                                 cifs_sb->local_nls, &num_referrals, &referrals);
251                                         cFYI(1,("Get DFS for %s rc = %d ",tmp_path, rc));
252                                         if((num_referrals == 0) && (rc == 0))
253                                                 rc = -EACCES;
254                                         else {
255                                                 cFYI(1,("num referral: %d",num_referrals));
256                                                 if(referrals) {
257                                                         cFYI(1,("referral string: %s ",referrals));
258                                                         strncpy(tmpbuffer, referrals, len-1);                            
259                                                 }
260                                         }
261                                         if(referrals)
262                                                 kfree(referrals);
263                                         kfree(tmp_path);
264                                         if(referrals) {
265                                                 kfree(referrals);
266                                         }
267                                 }
268                                 /* BB add code like else decode referrals then memcpy to
269                                   tmpbuffer and free referrals string array BB */
270                         }
271                 }
272         }
273         /* BB Anything else to do to handle recursive links? */
274         /* BB Should we be using page ops here? */
275
276         /* BB null terminate returned string in pBuffer? BB */
277         if (rc == 0) {
278                 rc = vfs_readlink(direntry, pBuffer, len, tmpbuffer);
279                 cFYI(1,
280                      ("vfs_readlink called from cifs_readlink returned %d",
281                       rc));
282         }
283
284         if (tmpbuffer) {
285                 kfree(tmpbuffer);
286         }
287         if (full_path) {
288                 kfree(full_path);
289         }
290         FreeXid(xid);
291         return rc;
292 }