a5c2812ead6831faf5e97645ab770384fcca096d
[cascardo/linux.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
32 /*
33  * M-F Symlink Functions - Begin
34  */
35
36 #define CIFS_MF_SYMLINK_LEN_OFFSET (4+1)
37 #define CIFS_MF_SYMLINK_MD5_OFFSET (CIFS_MF_SYMLINK_LEN_OFFSET+(4+1))
38 #define CIFS_MF_SYMLINK_LINK_OFFSET (CIFS_MF_SYMLINK_MD5_OFFSET+(32+1))
39 #define CIFS_MF_SYMLINK_LINK_MAXLEN (1024)
40 #define CIFS_MF_SYMLINK_FILE_SIZE \
41         (CIFS_MF_SYMLINK_LINK_OFFSET + CIFS_MF_SYMLINK_LINK_MAXLEN)
42
43 #define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
44 #define CIFS_MF_SYMLINK_MD5_FORMAT \
45         "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n"
46 #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) \
47         md5_hash[0],  md5_hash[1],  md5_hash[2],  md5_hash[3], \
48         md5_hash[4],  md5_hash[5],  md5_hash[6],  md5_hash[7], \
49         md5_hash[8],  md5_hash[9],  md5_hash[10], md5_hash[11],\
50         md5_hash[12], md5_hash[13], md5_hash[14], md5_hash[15]
51
52 static int
53 symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash)
54 {
55         int rc;
56         unsigned int size;
57         struct crypto_shash *md5;
58         struct sdesc *sdescmd5;
59
60         md5 = crypto_alloc_shash("md5", 0, 0);
61         if (IS_ERR(md5)) {
62                 rc = PTR_ERR(md5);
63                 cifs_dbg(VFS, "%s: Crypto md5 allocation error %d\n",
64                          __func__, rc);
65                 return rc;
66         }
67         size = sizeof(struct shash_desc) + crypto_shash_descsize(md5);
68         sdescmd5 = kmalloc(size, GFP_KERNEL);
69         if (!sdescmd5) {
70                 rc = -ENOMEM;
71                 goto symlink_hash_err;
72         }
73         sdescmd5->shash.tfm = md5;
74         sdescmd5->shash.flags = 0x0;
75
76         rc = crypto_shash_init(&sdescmd5->shash);
77         if (rc) {
78                 cifs_dbg(VFS, "%s: Could not init md5 shash\n", __func__);
79                 goto symlink_hash_err;
80         }
81         rc = crypto_shash_update(&sdescmd5->shash, link_str, link_len);
82         if (rc) {
83                 cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
84                 goto symlink_hash_err;
85         }
86         rc = crypto_shash_final(&sdescmd5->shash, md5_hash);
87         if (rc)
88                 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
89
90 symlink_hash_err:
91         crypto_free_shash(md5);
92         kfree(sdescmd5);
93
94         return rc;
95 }
96
97 static int
98 parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len,
99                  char **_link_str)
100 {
101         int rc;
102         unsigned int link_len;
103         const char *md5_str1;
104         const char *link_str;
105         u8 md5_hash[16];
106         char md5_str2[34];
107
108         if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
109                 return -EINVAL;
110
111         md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
112         link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
113
114         rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
115         if (rc != 1)
116                 return -EINVAL;
117
118         rc = symlink_hash(link_len, link_str, md5_hash);
119         if (rc) {
120                 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
121                 return rc;
122         }
123
124         snprintf(md5_str2, sizeof(md5_str2),
125                  CIFS_MF_SYMLINK_MD5_FORMAT,
126                  CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
127
128         if (strncmp(md5_str1, md5_str2, 17) != 0)
129                 return -EINVAL;
130
131         if (_link_str) {
132                 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
133                 if (!*_link_str)
134                         return -ENOMEM;
135         }
136
137         *_link_len = link_len;
138         return 0;
139 }
140
141 static int
142 format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
143 {
144         int rc;
145         unsigned int link_len;
146         unsigned int ofs;
147         u8 md5_hash[16];
148
149         if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
150                 return -EINVAL;
151
152         link_len = strlen(link_str);
153
154         if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
155                 return -ENAMETOOLONG;
156
157         rc = symlink_hash(link_len, link_str, md5_hash);
158         if (rc) {
159                 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
160                 return rc;
161         }
162
163         snprintf(buf, buf_len,
164                  CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
165                  link_len,
166                  CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
167
168         ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
169         memcpy(buf + ofs, link_str, link_len);
170
171         ofs += link_len;
172         if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
173                 buf[ofs] = '\n';
174                 ofs++;
175         }
176
177         while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
178                 buf[ofs] = ' ';
179                 ofs++;
180         }
181
182         return 0;
183 }
184
185 bool
186 couldbe_mf_symlink(const struct cifs_fattr *fattr)
187 {
188         if (!S_ISREG(fattr->cf_mode))
189                 /* it's not a symlink */
190                 return false;
191
192         if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
193                 /* it's not a symlink */
194                 return false;
195
196         return true;
197 }
198
199 static int
200 create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
201                   struct cifs_sb_info *cifs_sb, const char *fromName,
202                   const char *toName)
203 {
204         int rc;
205         u8 *buf;
206         unsigned int bytes_written = 0;
207
208         buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
209         if (!buf)
210                 return -ENOMEM;
211
212         rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
213         if (rc)
214                 goto out;
215
216         if (tcon->ses->server->ops->create_mf_symlink)
217                 rc = tcon->ses->server->ops->create_mf_symlink(xid, tcon,
218                                         cifs_sb, fromName, buf, &bytes_written);
219         else
220                 rc = -EOPNOTSUPP;
221
222         if (rc)
223                 goto out;
224
225         if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
226                 rc = -EIO;
227 out:
228         kfree(buf);
229         return rc;
230 }
231
232 static int
233 query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
234                  struct cifs_sb_info *cifs_sb, const unsigned char *path,
235                  char **symlinkinfo)
236 {
237         int rc;
238         u8 *buf = NULL;
239         unsigned int link_len = 0;
240         unsigned int bytes_read = 0;
241
242         buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
243         if (!buf)
244                 return -ENOMEM;
245
246         if (tcon->ses->server->ops->query_mf_symlink)
247                 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
248                                               cifs_sb, path, buf, &bytes_read);
249         else
250                 rc = -ENOSYS;
251
252         if (rc)
253                 goto out;
254
255         if (bytes_read == 0) { /* not a symlink */
256                 rc = -EINVAL;
257                 goto out;
258         }
259
260         rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo);
261 out:
262         kfree(buf);
263         return rc;
264 }
265
266 int
267 check_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
268                  struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
269                  const unsigned char *path)
270 {
271         int rc;
272         u8 *buf = NULL;
273         unsigned int link_len = 0;
274         unsigned int bytes_read = 0;
275
276         if (!couldbe_mf_symlink(fattr))
277                 /* it's not a symlink */
278                 return 0;
279
280         buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
281         if (!buf)
282                 return -ENOMEM;
283
284         if (tcon->ses->server->ops->query_mf_symlink)
285                 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
286                                               cifs_sb, path, buf, &bytes_read);
287         else
288                 rc = -ENOSYS;
289
290         if (rc)
291                 goto out;
292
293         if (bytes_read == 0) /* not a symlink */
294                 goto out;
295
296         rc = parse_mf_symlink(buf, bytes_read, &link_len, NULL);
297         if (rc == -EINVAL) {
298                 /* it's not a symlink */
299                 rc = 0;
300                 goto out;
301         }
302
303         if (rc != 0)
304                 goto out;
305
306         /* it is a symlink */
307         fattr->cf_eof = link_len;
308         fattr->cf_mode &= ~S_IFMT;
309         fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
310         fattr->cf_dtype = DT_LNK;
311 out:
312         kfree(buf);
313         return rc;
314 }
315
316 /*
317  * SMB 1.0 Protocol specific functions
318  */
319
320 int
321 cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
322                       struct cifs_sb_info *cifs_sb, const unsigned char *path,
323                       char *pbuf, unsigned int *pbytes_read)
324 {
325         int rc;
326         int oplock = 0;
327         struct cifs_fid fid;
328         struct cifs_open_parms oparms;
329         struct cifs_io_parms io_parms;
330         int buf_type = CIFS_NO_BUFFER;
331         FILE_ALL_INFO file_info;
332
333         oparms.tcon = tcon;
334         oparms.cifs_sb = cifs_sb;
335         oparms.desired_access = GENERIC_READ;
336         oparms.create_options = CREATE_NOT_DIR;
337         oparms.disposition = FILE_OPEN;
338         oparms.path = path;
339         oparms.fid = &fid;
340         oparms.reconnect = false;
341
342         rc = CIFS_open(xid, &oparms, &oplock, &file_info);
343         if (rc)
344                 return rc;
345
346         if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE))
347                 /* it's not a symlink */
348                 goto out;
349
350         io_parms.netfid = fid.netfid;
351         io_parms.pid = current->tgid;
352         io_parms.tcon = tcon;
353         io_parms.offset = 0;
354         io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
355
356         rc = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
357 out:
358         CIFSSMBClose(xid, tcon, fid.netfid);
359         return rc;
360 }
361
362 int
363 cifs_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
364                        struct cifs_sb_info *cifs_sb, const unsigned char *path,
365                        char *pbuf, unsigned int *pbytes_written)
366 {
367         int rc;
368         int oplock = 0;
369         struct cifs_fid fid;
370         struct cifs_open_parms oparms;
371         struct cifs_io_parms io_parms;
372         int create_options = CREATE_NOT_DIR;
373
374         if (backup_cred(cifs_sb))
375                 create_options |= CREATE_OPEN_BACKUP_INTENT;
376
377         oparms.tcon = tcon;
378         oparms.cifs_sb = cifs_sb;
379         oparms.desired_access = GENERIC_WRITE;
380         oparms.create_options = create_options;
381         oparms.disposition = FILE_CREATE;
382         oparms.path = path;
383         oparms.fid = &fid;
384         oparms.reconnect = false;
385
386         rc = CIFS_open(xid, &oparms, &oplock, NULL);
387         if (rc)
388                 return rc;
389
390         io_parms.netfid = fid.netfid;
391         io_parms.pid = current->tgid;
392         io_parms.tcon = tcon;
393         io_parms.offset = 0;
394         io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
395
396         rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf, NULL, 0);
397         CIFSSMBClose(xid, tcon, fid.netfid);
398         return rc;
399 }
400
401 /*
402  * M-F Symlink Functions - End
403  */
404
405 int
406 cifs_hardlink(struct dentry *old_file, struct inode *inode,
407               struct dentry *direntry)
408 {
409         int rc = -EACCES;
410         unsigned int xid;
411         char *from_name = NULL;
412         char *to_name = NULL;
413         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
414         struct tcon_link *tlink;
415         struct cifs_tcon *tcon;
416         struct TCP_Server_Info *server;
417         struct cifsInodeInfo *cifsInode;
418
419         tlink = cifs_sb_tlink(cifs_sb);
420         if (IS_ERR(tlink))
421                 return PTR_ERR(tlink);
422         tcon = tlink_tcon(tlink);
423
424         xid = get_xid();
425
426         from_name = build_path_from_dentry(old_file);
427         to_name = build_path_from_dentry(direntry);
428         if ((from_name == NULL) || (to_name == NULL)) {
429                 rc = -ENOMEM;
430                 goto cifs_hl_exit;
431         }
432
433         if (tcon->unix_ext)
434                 rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
435                                             cifs_sb->local_nls,
436                                             cifs_sb->mnt_cifs_flags &
437                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
438         else {
439                 server = tcon->ses->server;
440                 if (!server->ops->create_hardlink) {
441                         rc = -ENOSYS;
442                         goto cifs_hl_exit;
443                 }
444                 rc = server->ops->create_hardlink(xid, tcon, from_name, to_name,
445                                                   cifs_sb);
446                 if ((rc == -EIO) || (rc == -EINVAL))
447                         rc = -EOPNOTSUPP;
448         }
449
450         d_drop(direntry);       /* force new lookup from server of target */
451
452         /*
453          * if source file is cached (oplocked) revalidate will not go to server
454          * until the file is closed or oplock broken so update nlinks locally
455          */
456         if (old_file->d_inode) {
457                 cifsInode = CIFS_I(old_file->d_inode);
458                 if (rc == 0) {
459                         spin_lock(&old_file->d_inode->i_lock);
460                         inc_nlink(old_file->d_inode);
461                         spin_unlock(&old_file->d_inode->i_lock);
462                         /*
463                          * BB should we make this contingent on superblock flag
464                          * NOATIME?
465                          */
466                         /* old_file->d_inode->i_ctime = CURRENT_TIME; */
467                         /*
468                          * parent dir timestamps will update from srv within a
469                          * second, would it really be worth it to set the parent
470                          * dir cifs inode time to zero to force revalidate
471                          * (faster) for it too?
472                          */
473                 }
474                 /*
475                  * if not oplocked will force revalidate to get info on source
476                  * file from srv
477                  */
478                 cifsInode->time = 0;
479
480                 /*
481                  * Will update parent dir timestamps from srv within a second.
482                  * Would it really be worth it to set the parent dir (cifs
483                  * inode) time field to zero to force revalidate on parent
484                  * directory faster ie
485                  *
486                  * CIFS_I(inode)->time = 0;
487                  */
488         }
489
490 cifs_hl_exit:
491         kfree(from_name);
492         kfree(to_name);
493         free_xid(xid);
494         cifs_put_tlink(tlink);
495         return rc;
496 }
497
498 void *
499 cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
500 {
501         struct inode *inode = direntry->d_inode;
502         int rc = -ENOMEM;
503         unsigned int xid;
504         char *full_path = NULL;
505         char *target_path = NULL;
506         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
507         struct tcon_link *tlink = NULL;
508         struct cifs_tcon *tcon;
509         struct TCP_Server_Info *server;
510
511         xid = get_xid();
512
513         tlink = cifs_sb_tlink(cifs_sb);
514         if (IS_ERR(tlink)) {
515                 rc = PTR_ERR(tlink);
516                 tlink = NULL;
517                 goto out;
518         }
519         tcon = tlink_tcon(tlink);
520         server = tcon->ses->server;
521
522         full_path = build_path_from_dentry(direntry);
523         if (!full_path)
524                 goto out;
525
526         cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
527
528         rc = -EACCES;
529         /*
530          * First try Minshall+French Symlinks, if configured
531          * and fallback to UNIX Extensions Symlinks.
532          */
533         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
534                 rc = query_mf_symlink(xid, tcon, cifs_sb, full_path,
535                                       &target_path);
536
537         if (rc != 0 && server->ops->query_symlink)
538                 rc = server->ops->query_symlink(xid, tcon, full_path,
539                                                 &target_path, cifs_sb);
540
541         kfree(full_path);
542 out:
543         if (rc != 0) {
544                 kfree(target_path);
545                 target_path = ERR_PTR(rc);
546         }
547
548         free_xid(xid);
549         if (tlink)
550                 cifs_put_tlink(tlink);
551         nd_set_link(nd, target_path);
552         return NULL;
553 }
554
555 int
556 cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
557 {
558         int rc = -EOPNOTSUPP;
559         unsigned int xid;
560         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
561         struct tcon_link *tlink;
562         struct cifs_tcon *pTcon;
563         char *full_path = NULL;
564         struct inode *newinode = NULL;
565
566         xid = get_xid();
567
568         tlink = cifs_sb_tlink(cifs_sb);
569         if (IS_ERR(tlink)) {
570                 rc = PTR_ERR(tlink);
571                 goto symlink_exit;
572         }
573         pTcon = tlink_tcon(tlink);
574
575         full_path = build_path_from_dentry(direntry);
576         if (full_path == NULL) {
577                 rc = -ENOMEM;
578                 goto symlink_exit;
579         }
580
581         cifs_dbg(FYI, "Full path: %s\n", full_path);
582         cifs_dbg(FYI, "symname is %s\n", symname);
583
584         /* BB what if DFS and this volume is on different share? BB */
585         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
586                 rc = create_mf_symlink(xid, pTcon, cifs_sb, full_path, symname);
587         else if (pTcon->unix_ext)
588                 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
589                                            cifs_sb->local_nls);
590         /* else
591            rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
592                                         cifs_sb_target->local_nls); */
593
594         if (rc == 0) {
595                 if (pTcon->unix_ext)
596                         rc = cifs_get_inode_info_unix(&newinode, full_path,
597                                                       inode->i_sb, xid);
598                 else
599                         rc = cifs_get_inode_info(&newinode, full_path, NULL,
600                                                  inode->i_sb, xid, NULL);
601
602                 if (rc != 0) {
603                         cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
604                                  rc);
605                 } else {
606                         d_instantiate(direntry, newinode);
607                 }
608         }
609 symlink_exit:
610         kfree(full_path);
611         cifs_put_tlink(tlink);
612         free_xid(xid);
613         return rc;
614 }