cifs: implement CIFSCreateMFSymLink()
[pandora-kernel.git] / fs / cifs / link.c
1 /*
2  *   fs/cifs/link.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2008
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 <linux/slab.h>
24 #include <linux/namei.h>
25 #include "cifsfs.h"
26 #include "cifspdu.h"
27 #include "cifsglob.h"
28 #include "cifsproto.h"
29 #include "cifs_debug.h"
30 #include "cifs_fs_sb.h"
31 #include "md5.h"
32
33 #define CIFS_MF_SYMLINK_LEN_OFFSET (4+1)
34 #define CIFS_MF_SYMLINK_MD5_OFFSET (CIFS_MF_SYMLINK_LEN_OFFSET+(4+1))
35 #define CIFS_MF_SYMLINK_LINK_OFFSET (CIFS_MF_SYMLINK_MD5_OFFSET+(32+1))
36 #define CIFS_MF_SYMLINK_LINK_MAXLEN (1024)
37 #define CIFS_MF_SYMLINK_FILE_SIZE \
38         (CIFS_MF_SYMLINK_LINK_OFFSET + CIFS_MF_SYMLINK_LINK_MAXLEN)
39
40 #define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
41 #define CIFS_MF_SYMLINK_MD5_FORMAT \
42         "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n"
43 #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) \
44         md5_hash[0],  md5_hash[1],  md5_hash[2],  md5_hash[3], \
45         md5_hash[4],  md5_hash[5],  md5_hash[6],  md5_hash[7], \
46         md5_hash[8],  md5_hash[9],  md5_hash[10], md5_hash[11],\
47         md5_hash[12], md5_hash[13], md5_hash[14], md5_hash[15]
48
49 static int
50 CIFSParseMFSymlink(const u8 *buf,
51                    unsigned int buf_len,
52                    unsigned int *_link_len,
53                    char **_link_str)
54 {
55         int rc;
56         unsigned int link_len;
57         const char *md5_str1;
58         const char *link_str;
59         struct MD5Context md5_ctx;
60         u8 md5_hash[16];
61         char md5_str2[34];
62
63         if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
64                 return -EINVAL;
65
66         md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
67         link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
68
69         rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
70         if (rc != 1)
71                 return -EINVAL;
72
73         cifs_MD5_init(&md5_ctx);
74         cifs_MD5_update(&md5_ctx, (const u8 *)link_str, link_len);
75         cifs_MD5_final(md5_hash, &md5_ctx);
76
77         snprintf(md5_str2, sizeof(md5_str2),
78                  CIFS_MF_SYMLINK_MD5_FORMAT,
79                  CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
80
81         if (strncmp(md5_str1, md5_str2, 17) != 0)
82                 return -EINVAL;
83
84         if (_link_str) {
85                 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
86                 if (!*_link_str)
87                         return -ENOMEM;
88         }
89
90         *_link_len = link_len;
91         return 0;
92 }
93
94 static int
95 CIFSFormatMFSymlink(u8 *buf, unsigned int buf_len, const char *link_str)
96 {
97         unsigned int link_len;
98         unsigned int ofs;
99         struct MD5Context md5_ctx;
100         u8 md5_hash[16];
101
102         if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
103                 return -EINVAL;
104
105         link_len = strlen(link_str);
106
107         if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
108                 return -ENAMETOOLONG;
109
110         cifs_MD5_init(&md5_ctx);
111         cifs_MD5_update(&md5_ctx, (const u8 *)link_str, link_len);
112         cifs_MD5_final(md5_hash, &md5_ctx);
113
114         snprintf(buf, buf_len,
115                  CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
116                  link_len,
117                  CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
118
119         ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
120         memcpy(buf + ofs, link_str, link_len);
121
122         ofs += link_len;
123         if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
124                 buf[ofs] = '\n';
125                 ofs++;
126         }
127
128         while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
129                 buf[ofs] = ' ';
130                 ofs++;
131         }
132
133         return 0;
134 }
135
136 static int
137 CIFSCreateMFSymLink(const int xid, struct cifsTconInfo *tcon,
138                     const char *fromName, const char *toName,
139                     const struct nls_table *nls_codepage, int remap)
140 {
141         int rc;
142         int oplock = 0;
143         __u16 netfid = 0;
144         u8 *buf;
145         unsigned int bytes_written = 0;
146
147         buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
148         if (!buf)
149                 return -ENOMEM;
150
151         rc = CIFSFormatMFSymlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
152         if (rc != 0) {
153                 kfree(buf);
154                 return rc;
155         }
156
157         rc = CIFSSMBOpen(xid, tcon, fromName, FILE_CREATE, GENERIC_WRITE,
158                          CREATE_NOT_DIR, &netfid, &oplock, NULL,
159                          nls_codepage, remap);
160         if (rc != 0) {
161                 kfree(buf);
162                 return rc;
163         }
164
165         rc = CIFSSMBWrite(xid, tcon, netfid,
166                           CIFS_MF_SYMLINK_FILE_SIZE /* length */,
167                           0 /* offset */,
168                           &bytes_written, buf, NULL, 0);
169         CIFSSMBClose(xid, tcon, netfid);
170         kfree(buf);
171         if (rc != 0)
172                 return rc;
173
174         if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
175                 return -EIO;
176
177         return 0;
178 }
179
180 static int
181 CIFSQueryMFSymLink(const int xid, struct cifsTconInfo *tcon,
182                    const unsigned char *searchName, char **symlinkinfo,
183                    const struct nls_table *nls_codepage, int remap)
184 {
185         int rc;
186         int oplock = 0;
187         __u16 netfid = 0;
188         u8 *buf;
189         char *pbuf;
190         unsigned int bytes_read = 0;
191         int buf_type = CIFS_NO_BUFFER;
192         unsigned int link_len = 0;
193         FILE_ALL_INFO file_info;
194
195         rc = CIFSSMBOpen(xid, tcon, searchName, FILE_OPEN, GENERIC_READ,
196                          CREATE_NOT_DIR, &netfid, &oplock, &file_info,
197                          nls_codepage, remap);
198         if (rc != 0)
199                 return rc;
200
201         if (file_info.EndOfFile != CIFS_MF_SYMLINK_FILE_SIZE) {
202                 CIFSSMBClose(xid, tcon, netfid);
203                 /* it's not a symlink */
204                 return -EINVAL;
205         }
206
207         buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
208         if (!buf)
209                 return -ENOMEM;
210         pbuf = buf;
211
212         rc = CIFSSMBRead(xid, tcon, netfid,
213                          CIFS_MF_SYMLINK_FILE_SIZE /* length */,
214                          0 /* offset */,
215                          &bytes_read, &pbuf, &buf_type);
216         CIFSSMBClose(xid, tcon, netfid);
217         if (rc != 0) {
218                 kfree(buf);
219                 return rc;
220         }
221
222         rc = CIFSParseMFSymlink(buf, bytes_read, &link_len, symlinkinfo);
223         kfree(buf);
224         if (rc != 0)
225                 return rc;
226
227         return 0;
228 }
229
230 bool
231 CIFSCouldBeMFSymlink(const struct cifs_fattr *fattr)
232 {
233         if (!(fattr->cf_mode & S_IFREG))
234                 /* it's not a symlink */
235                 return false;
236
237         if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
238                 /* it's not a symlink */
239                 return false;
240
241         return true;
242 }
243
244 int
245 CIFSCheckMFSymlink(struct cifs_fattr *fattr,
246                    const unsigned char *path,
247                    struct cifs_sb_info *cifs_sb, int xid)
248 {
249         int rc;
250         int oplock = 0;
251         __u16 netfid = 0;
252         struct cifsTconInfo *pTcon = cifs_sb->tcon;
253         u8 *buf;
254         char *pbuf;
255         unsigned int bytes_read = 0;
256         int buf_type = CIFS_NO_BUFFER;
257         unsigned int link_len = 0;
258         FILE_ALL_INFO file_info;
259
260         if (!CIFSCouldBeMFSymlink(fattr))
261                 /* it's not a symlink */
262                 return 0;
263
264         rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
265                          CREATE_NOT_DIR, &netfid, &oplock, &file_info,
266                          cifs_sb->local_nls,
267                          cifs_sb->mnt_cifs_flags &
268                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
269         if (rc != 0)
270                 return rc;
271
272         if (file_info.EndOfFile != CIFS_MF_SYMLINK_FILE_SIZE) {
273                 CIFSSMBClose(xid, pTcon, netfid);
274                 /* it's not a symlink */
275                 return 0;
276         }
277
278         buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
279         if (!buf)
280                 return -ENOMEM;
281         pbuf = buf;
282
283         rc = CIFSSMBRead(xid, pTcon, netfid,
284                          CIFS_MF_SYMLINK_FILE_SIZE /* length */,
285                          0 /* offset */,
286                          &bytes_read, &pbuf, &buf_type);
287         CIFSSMBClose(xid, pTcon, netfid);
288         if (rc != 0) {
289                 kfree(buf);
290                 return rc;
291         }
292
293         rc = CIFSParseMFSymlink(buf, bytes_read, &link_len, NULL);
294         kfree(buf);
295         if (rc == -EINVAL)
296                 /* it's not a symlink */
297                 return 0;
298         if (rc != 0)
299                 return rc;
300
301         /* it is a symlink */
302         fattr->cf_eof = link_len;
303         fattr->cf_mode &= ~S_IFMT;
304         fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
305         fattr->cf_dtype = DT_LNK;
306         return 0;
307 }
308
309 int
310 cifs_hardlink(struct dentry *old_file, struct inode *inode,
311               struct dentry *direntry)
312 {
313         int rc = -EACCES;
314         int xid;
315         char *fromName = NULL;
316         char *toName = NULL;
317         struct cifs_sb_info *cifs_sb_target;
318         struct cifsTconInfo *pTcon;
319         struct cifsInodeInfo *cifsInode;
320
321         xid = GetXid();
322
323         cifs_sb_target = CIFS_SB(inode->i_sb);
324         pTcon = cifs_sb_target->tcon;
325
326 /* No need to check for cross device links since server will do that
327    BB note DFS case in future though (when we may have to check) */
328
329         fromName = build_path_from_dentry(old_file);
330         toName = build_path_from_dentry(direntry);
331         if ((fromName == NULL) || (toName == NULL)) {
332                 rc = -ENOMEM;
333                 goto cifs_hl_exit;
334         }
335
336 /*      if (cifs_sb_target->tcon->ses->capabilities & CAP_UNIX)*/
337         if (pTcon->unix_ext)
338                 rc = CIFSUnixCreateHardLink(xid, pTcon, fromName, toName,
339                                             cifs_sb_target->local_nls,
340                                             cifs_sb_target->mnt_cifs_flags &
341                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
342         else {
343                 rc = CIFSCreateHardLink(xid, pTcon, fromName, toName,
344                                         cifs_sb_target->local_nls,
345                                         cifs_sb_target->mnt_cifs_flags &
346                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
347                 if ((rc == -EIO) || (rc == -EINVAL))
348                         rc = -EOPNOTSUPP;
349         }
350
351         d_drop(direntry);       /* force new lookup from server of target */
352
353         /* if source file is cached (oplocked) revalidate will not go to server
354            until the file is closed or oplock broken so update nlinks locally */
355         if (old_file->d_inode) {
356                 cifsInode = CIFS_I(old_file->d_inode);
357                 if (rc == 0) {
358                         old_file->d_inode->i_nlink++;
359 /* BB should we make this contingent on superblock flag NOATIME? */
360 /*                      old_file->d_inode->i_ctime = CURRENT_TIME;*/
361                         /* parent dir timestamps will update from srv
362                         within a second, would it really be worth it
363                         to set the parent dir cifs inode time to zero
364                         to force revalidate (faster) for it too? */
365                 }
366                 /* if not oplocked will force revalidate to get info
367                    on source file from srv */
368                 cifsInode->time = 0;
369
370                 /* Will update parent dir timestamps from srv within a second.
371                    Would it really be worth it to set the parent dir (cifs
372                    inode) time field to zero to force revalidate on parent
373                    directory faster ie
374                         CIFS_I(inode)->time = 0;  */
375         }
376
377 cifs_hl_exit:
378         kfree(fromName);
379         kfree(toName);
380         FreeXid(xid);
381         return rc;
382 }
383
384 void *
385 cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
386 {
387         struct inode *inode = direntry->d_inode;
388         int rc = -ENOMEM;
389         int xid;
390         char *full_path = NULL;
391         char *target_path = NULL;
392         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
393         struct cifsTconInfo *tcon = cifs_sb->tcon;
394
395         xid = GetXid();
396
397         /*
398          * For now, we just handle symlinks with unix extensions enabled.
399          * Eventually we should handle NTFS reparse points, and MacOS
400          * symlink support. For instance...
401          *
402          * rc = CIFSSMBQueryReparseLinkInfo(...)
403          *
404          * For now, just return -EACCES when the server doesn't support posix
405          * extensions. Note that we still allow querying symlinks when posix
406          * extensions are manually disabled. We could disable these as well
407          * but there doesn't seem to be any harm in allowing the client to
408          * read them.
409          */
410         if (!(tcon->ses->capabilities & CAP_UNIX)) {
411                 rc = -EACCES;
412                 goto out;
413         }
414
415         full_path = build_path_from_dentry(direntry);
416         if (!full_path)
417                 goto out;
418
419         cFYI(1, "Full path: %s inode = 0x%p", full_path, inode);
420
421         rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, &target_path,
422                                      cifs_sb->local_nls);
423         kfree(full_path);
424 out:
425         if (rc != 0) {
426                 kfree(target_path);
427                 target_path = ERR_PTR(rc);
428         }
429
430         FreeXid(xid);
431         nd_set_link(nd, target_path);
432         return NULL;
433 }
434
435 int
436 cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
437 {
438         int rc = -EOPNOTSUPP;
439         int xid;
440         struct cifs_sb_info *cifs_sb;
441         struct cifsTconInfo *pTcon;
442         char *full_path = NULL;
443         struct inode *newinode = NULL;
444
445         xid = GetXid();
446
447         cifs_sb = CIFS_SB(inode->i_sb);
448         pTcon = cifs_sb->tcon;
449
450         full_path = build_path_from_dentry(direntry);
451
452         if (full_path == NULL) {
453                 rc = -ENOMEM;
454                 FreeXid(xid);
455                 return rc;
456         }
457
458         cFYI(1, "Full path: %s", full_path);
459         cFYI(1, "symname is %s", symname);
460
461         /* BB what if DFS and this volume is on different share? BB */
462         if (pTcon->unix_ext)
463                 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
464                                            cifs_sb->local_nls);
465         /* else
466            rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
467                                         cifs_sb_target->local_nls); */
468
469         if (rc == 0) {
470                 if (pTcon->unix_ext)
471                         rc = cifs_get_inode_info_unix(&newinode, full_path,
472                                                       inode->i_sb, xid);
473                 else
474                         rc = cifs_get_inode_info(&newinode, full_path, NULL,
475                                                  inode->i_sb, xid, NULL);
476
477                 if (rc != 0) {
478                         cFYI(1, "Create symlink ok, getinodeinfo fail rc = %d",
479                               rc);
480                 } else {
481                         if (pTcon->nocase)
482                                 direntry->d_op = &cifs_ci_dentry_ops;
483                         else
484                                 direntry->d_op = &cifs_dentry_ops;
485                         d_instantiate(direntry, newinode);
486                 }
487         }
488
489         kfree(full_path);
490         FreeXid(xid);
491         return rc;
492 }
493
494 void cifs_put_link(struct dentry *direntry, struct nameidata *nd, void *cookie)
495 {
496         char *p = nd_get_link(nd);
497         if (!IS_ERR(p))
498                 kfree(p);
499 }