link.c 5.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *   fs/cifs/link.c
 *
S
Steve French 已提交
4
 *   Copyright (C) International Business Machines  Corp., 2002,2008
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 *   Author(s): Steve French (sfrench@us.ibm.com)
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/namei.h>
#include "cifsfs.h"
#include "cifspdu.h"
#include "cifsglob.h"
#include "cifsproto.h"
#include "cifs_debug.h"
#include "cifs_fs_sb.h"

int
cifs_hardlink(struct dentry *old_file, struct inode *inode,
	      struct dentry *direntry)
{
	int rc = -EACCES;
	int xid;
	char *fromName = NULL;
	char *toName = NULL;
	struct cifs_sb_info *cifs_sb_target;
	struct cifsTconInfo *pTcon;
	struct cifsInodeInfo *cifsInode;

	xid = GetXid();

	cifs_sb_target = CIFS_SB(inode->i_sb);
	pTcon = cifs_sb_target->tcon;

/* 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) */

51 52
	fromName = build_path_from_dentry(old_file);
	toName = build_path_from_dentry(direntry);
S
Steve French 已提交
53
	if ((fromName == NULL) || (toName == NULL)) {
L
Linus Torvalds 已提交
54 55 56 57
		rc = -ENOMEM;
		goto cifs_hl_exit;
	}

58 59
/*	if (cifs_sb_target->tcon->ses->capabilities & CAP_UNIX)*/
	if (pTcon->unix_ext)
L
Linus Torvalds 已提交
60
		rc = CIFSUnixCreateHardLink(xid, pTcon, fromName, toName,
S
Steve French 已提交
61
					    cifs_sb_target->local_nls,
62 63
					    cifs_sb_target->mnt_cifs_flags &
						CIFS_MOUNT_MAP_SPECIAL_CHR);
L
Linus Torvalds 已提交
64 65
	else {
		rc = CIFSCreateHardLink(xid, pTcon, fromName, toName,
S
Steve French 已提交
66
					cifs_sb_target->local_nls,
67 68
					cifs_sb_target->mnt_cifs_flags &
						CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
69 70
		if ((rc == -EIO) || (rc == -EINVAL))
			rc = -EOPNOTSUPP;
L
Linus Torvalds 已提交
71 72
	}

73 74 75 76
	d_drop(direntry);	/* force new lookup from server of target */

	/* if source file is cached (oplocked) revalidate will not go to server
	   until the file is closed or oplock broken so update nlinks locally */
S
Steve French 已提交
77
	if (old_file->d_inode) {
78
		cifsInode = CIFS_I(old_file->d_inode);
S
Steve French 已提交
79
		if (rc == 0) {
80
			old_file->d_inode->i_nlink++;
81 82
/* BB should we make this contingent on superblock flag NOATIME? */
/*			old_file->d_inode->i_ctime = CURRENT_TIME;*/
83 84 85 86 87
			/* parent dir timestamps will update from srv
			within a second, would it really be worth it
			to set the parent dir cifs inode time to zero
			to force revalidate (faster) for it too? */
		}
S
Steve French 已提交
88
		/* if not oplocked will force revalidate to get info
89 90 91
		   on source file from srv */
		cifsInode->time = 0;

S
Steve French 已提交
92
		/* Will update parent dir timestamps from srv within a second.
93 94
		   Would it really be worth it to set the parent dir (cifs
		   inode) time field to zero to force revalidate on parent
S
Steve French 已提交
95
		   directory faster ie
96
			CIFS_I(inode)->time = 0;  */
L
Linus Torvalds 已提交
97 98 99
	}

cifs_hl_exit:
J
Jesper Juhl 已提交
100 101
	kfree(fromName);
	kfree(toName);
L
Linus Torvalds 已提交
102 103 104 105
	FreeXid(xid);
	return rc;
}

106
void *
L
Linus Torvalds 已提交
107 108 109 110 111 112
cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
{
	struct inode *inode = direntry->d_inode;
	int rc = -EACCES;
	int xid;
	char *full_path = NULL;
S
Steve French 已提交
113
	char *target_path = ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
114 115 116 117 118
	struct cifs_sb_info *cifs_sb;
	struct cifsTconInfo *pTcon;

	xid = GetXid();

119
	full_path = build_path_from_dentry(direntry);
L
Linus Torvalds 已提交
120 121

	if (!full_path)
122
		goto out;
L
Linus Torvalds 已提交
123 124

	cFYI(1, ("Full path: %s inode = 0x%p", full_path, inode));
125 126
	cifs_sb = CIFS_SB(inode->i_sb);
	pTcon = cifs_sb->tcon;
L
Linus Torvalds 已提交
127

128 129 130 131 132 133
	/* We could change this to:
		if (pTcon->unix_ext)
	   but there does not seem any point in refusing to
	   get symlink info if we can, even if unix extensions
	   turned off for this mount */

L
Linus Torvalds 已提交
134 135
	if (pTcon->ses->capabilities & CAP_UNIX)
		rc = CIFSSMBUnixQuerySymLink(xid, pTcon, full_path,
136
					     &target_path,
L
Linus Torvalds 已提交
137 138
					     cifs_sb->local_nls);
	else {
139
		/* BB add read reparse point symlink code here */
L
Linus Torvalds 已提交
140 141 142 143 144
		/* rc = CIFSSMBQueryReparseLinkInfo */
		/* BB Add code to Query ReparsePoint info */
		/* BB Add MAC style xsymlink check here if enabled */
	}

145
	if (rc != 0) {
L
Linus Torvalds 已提交
146 147 148 149 150
		kfree(target_path);
		target_path = ERR_PTR(rc);
	}

	kfree(full_path);
151
out:
L
Linus Torvalds 已提交
152 153
	FreeXid(xid);
	nd_set_link(nd, target_path);
154
	return NULL;
L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
}

int
cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
{
	int rc = -EOPNOTSUPP;
	int xid;
	struct cifs_sb_info *cifs_sb;
	struct cifsTconInfo *pTcon;
	char *full_path = NULL;
	struct inode *newinode = NULL;

	xid = GetXid();

	cifs_sb = CIFS_SB(inode->i_sb);
	pTcon = cifs_sb->tcon;

172
	full_path = build_path_from_dentry(direntry);
L
Linus Torvalds 已提交
173

S
Steve French 已提交
174
	if (full_path == NULL) {
L
Linus Torvalds 已提交
175 176 177 178
		FreeXid(xid);
		return -ENOMEM;
	}

179
	cFYI(1, ("Full path: %s", full_path));
L
Linus Torvalds 已提交
180 181 182
	cFYI(1, ("symname is %s", symname));

	/* BB what if DFS and this volume is on different share? BB */
183
	if (pTcon->unix_ext)
L
Linus Torvalds 已提交
184 185 186
		rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
					   cifs_sb->local_nls);
	/* else
S
Steve French 已提交
187 188
	   rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
					cifs_sb_target->local_nls); */
L
Linus Torvalds 已提交
189 190

	if (rc == 0) {
191
		if (pTcon->unix_ext)
L
Linus Torvalds 已提交
192
			rc = cifs_get_inode_info_unix(&newinode, full_path,
S
Steve French 已提交
193
						      inode->i_sb, xid);
L
Linus Torvalds 已提交
194 195
		else
			rc = cifs_get_inode_info(&newinode, full_path, NULL,
196
						 inode->i_sb, xid, NULL);
L
Linus Torvalds 已提交
197 198

		if (rc != 0) {
199
			cFYI(1, ("Create symlink ok, getinodeinfo fail rc = %d",
L
Linus Torvalds 已提交
200 201
			      rc));
		} else {
202 203 204 205
			if (pTcon->nocase)
				direntry->d_op = &cifs_ci_dentry_ops;
			else
				direntry->d_op = &cifs_dentry_ops;
L
Linus Torvalds 已提交
206 207 208 209
			d_instantiate(direntry, newinode);
		}
	}

J
Jesper Juhl 已提交
210
	kfree(full_path);
L
Linus Torvalds 已提交
211 212 213 214
	FreeXid(xid);
	return rc;
}

215
void cifs_put_link(struct dentry *direntry, struct nameidata *nd, void *cookie)
L
Linus Torvalds 已提交
216 217 218 219 220
{
	char *p = nd_get_link(nd);
	if (!IS_ERR(p))
		kfree(p);
}