4 * Copyright (c) International Business Machines Corp., 2003, 2007
5 * Author(s): Steve French (sfrench@us.ibm.com)
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.
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.
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
23 #include <linux/posix_acl_xattr.h>
24 #include <linux/slab.h>
25 #include <linux/xattr.h>
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
32 #define MAX_EA_VALUE_SIZE 65535
33 #define CIFS_XATTR_DOS_ATTRIB "user.DosAttrib"
34 #define CIFS_XATTR_CIFS_ACL "system.cifs_acl"
36 /* BB need to add server (Samba e.g) support for security and trusted prefix */
38 int cifs_removexattr(struct dentry
*direntry
, const char *ea_name
)
41 #ifdef CONFIG_CIFS_XATTR
43 struct cifs_sb_info
*cifs_sb
;
44 struct tcon_link
*tlink
;
45 struct cifs_tcon
*pTcon
;
46 struct super_block
*sb
;
47 char *full_path
= NULL
;
51 if (direntry
->d_inode
== NULL
)
53 sb
= direntry
->d_inode
->i_sb
;
57 cifs_sb
= CIFS_SB(sb
);
58 tlink
= cifs_sb_tlink(cifs_sb
);
60 return PTR_ERR(tlink
);
61 pTcon
= tlink_tcon(tlink
);
65 full_path
= build_path_from_dentry(direntry
);
66 if (full_path
== NULL
) {
70 if (ea_name
== NULL
) {
71 cFYI(1, "Null xattr names not supported");
72 } else if (strncmp(ea_name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
)
73 && (strncmp(ea_name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
))) {
75 "illegal xattr request %s (only user namespace supported)",
77 /* BB what if no namespace prefix? */
78 /* Should we just pass them to server, except for
79 system and perhaps security prefixes? */
81 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_XATTR
)
84 ea_name
+= XATTR_USER_PREFIX_LEN
; /* skip past user. prefix */
85 rc
= CIFSSMBSetEA(xid
, pTcon
, full_path
, ea_name
, NULL
,
86 (__u16
)0, cifs_sb
->local_nls
,
87 cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
);
92 cifs_put_tlink(tlink
);
97 int cifs_setxattr(struct dentry
*direntry
, const char *ea_name
,
98 const void *ea_value
, size_t value_size
, int flags
)
100 int rc
= -EOPNOTSUPP
;
101 #ifdef CONFIG_CIFS_XATTR
103 struct cifs_sb_info
*cifs_sb
;
104 struct tcon_link
*tlink
;
105 struct cifs_tcon
*pTcon
;
106 struct super_block
*sb
;
108 struct cifs_ntsd
*pacl
;
110 if (direntry
== NULL
)
112 if (direntry
->d_inode
== NULL
)
114 sb
= direntry
->d_inode
->i_sb
;
118 cifs_sb
= CIFS_SB(sb
);
119 tlink
= cifs_sb_tlink(cifs_sb
);
121 return PTR_ERR(tlink
);
122 pTcon
= tlink_tcon(tlink
);
126 full_path
= build_path_from_dentry(direntry
);
127 if (full_path
== NULL
) {
131 /* return dos attributes as pseudo xattr */
132 /* return alt name if available as pseudo attr */
134 /* if proc/fs/cifs/streamstoxattr is set then
135 search server for EAs or streams to
137 if (value_size
> MAX_EA_VALUE_SIZE
) {
138 cFYI(1, "size of EA value too large");
143 if (ea_name
== NULL
) {
144 cFYI(1, "Null xattr names not supported");
145 } else if (strncmp(ea_name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
)
147 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_XATTR
)
149 if (strncmp(ea_name
, CIFS_XATTR_DOS_ATTRIB
, 14) == 0)
150 cFYI(1, "attempt to set cifs inode metadata");
152 ea_name
+= XATTR_USER_PREFIX_LEN
; /* skip past user. prefix */
153 rc
= CIFSSMBSetEA(xid
, pTcon
, full_path
, ea_name
, ea_value
,
154 (__u16
)value_size
, cifs_sb
->local_nls
,
155 cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
);
156 } else if (strncmp(ea_name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
)
158 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_XATTR
)
161 ea_name
+= XATTR_OS2_PREFIX_LEN
; /* skip past os2. prefix */
162 rc
= CIFSSMBSetEA(xid
, pTcon
, full_path
, ea_name
, ea_value
,
163 (__u16
)value_size
, cifs_sb
->local_nls
,
164 cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
);
165 } else if (strncmp(ea_name
, CIFS_XATTR_CIFS_ACL
,
166 strlen(CIFS_XATTR_CIFS_ACL
)) == 0) {
167 pacl
= kmalloc(value_size
, GFP_KERNEL
);
169 cFYI(1, "%s: Can't allocate memory for ACL",
173 #ifdef CONFIG_CIFS_ACL
174 memcpy(pacl
, ea_value
, value_size
);
175 rc
= set_cifs_acl(pacl
, value_size
,
176 direntry
->d_inode
, full_path
);
177 if (rc
== 0) /* force revalidate of the inode */
178 CIFS_I(direntry
->d_inode
)->time
= 0;
181 cFYI(1, "Set CIFS ACL not supported yet");
182 #endif /* CONFIG_CIFS_ACL */
186 temp
= strncmp(ea_name
, POSIX_ACL_XATTR_ACCESS
,
187 strlen(POSIX_ACL_XATTR_ACCESS
));
189 #ifdef CONFIG_CIFS_POSIX
190 if (sb
->s_flags
& MS_POSIXACL
)
191 rc
= CIFSSMBSetPosixACL(xid
, pTcon
, full_path
,
192 ea_value
, (const int)value_size
,
193 ACL_TYPE_ACCESS
, cifs_sb
->local_nls
,
194 cifs_sb
->mnt_cifs_flags
&
195 CIFS_MOUNT_MAP_SPECIAL_CHR
);
196 cFYI(1, "set POSIX ACL rc %d", rc
);
198 cFYI(1, "set POSIX ACL not supported");
200 } else if (strncmp(ea_name
, POSIX_ACL_XATTR_DEFAULT
,
201 strlen(POSIX_ACL_XATTR_DEFAULT
)) == 0) {
202 #ifdef CONFIG_CIFS_POSIX
203 if (sb
->s_flags
& MS_POSIXACL
)
204 rc
= CIFSSMBSetPosixACL(xid
, pTcon
, full_path
,
205 ea_value
, (const int)value_size
,
206 ACL_TYPE_DEFAULT
, cifs_sb
->local_nls
,
207 cifs_sb
->mnt_cifs_flags
&
208 CIFS_MOUNT_MAP_SPECIAL_CHR
);
209 cFYI(1, "set POSIX default ACL rc %d", rc
);
211 cFYI(1, "set default POSIX ACL not supported");
214 cFYI(1, "illegal xattr request %s (only user namespace"
215 " supported)", ea_name
);
216 /* BB what if no namespace prefix? */
217 /* Should we just pass them to server, except for
218 system and perhaps security prefixes? */
225 cifs_put_tlink(tlink
);
230 ssize_t
cifs_getxattr(struct dentry
*direntry
, const char *ea_name
,
231 void *ea_value
, size_t buf_size
)
233 ssize_t rc
= -EOPNOTSUPP
;
234 #ifdef CONFIG_CIFS_XATTR
236 struct cifs_sb_info
*cifs_sb
;
237 struct tcon_link
*tlink
;
238 struct cifs_tcon
*pTcon
;
239 struct super_block
*sb
;
242 if (direntry
== NULL
)
244 if (direntry
->d_inode
== NULL
)
246 sb
= direntry
->d_inode
->i_sb
;
250 cifs_sb
= CIFS_SB(sb
);
251 tlink
= cifs_sb_tlink(cifs_sb
);
253 return PTR_ERR(tlink
);
254 pTcon
= tlink_tcon(tlink
);
258 full_path
= build_path_from_dentry(direntry
);
259 if (full_path
== NULL
) {
263 /* return dos attributes as pseudo xattr */
264 /* return alt name if available as pseudo attr */
265 if (ea_name
== NULL
) {
266 cFYI(1, "Null xattr names not supported");
267 } else if (strncmp(ea_name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
)
269 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_XATTR
)
272 if (strncmp(ea_name
, CIFS_XATTR_DOS_ATTRIB
, 14) == 0) {
273 cFYI(1, "attempt to query cifs inode metadata");
274 /* revalidate/getattr then populate from inode */
275 } /* BB add else when above is implemented */
276 ea_name
+= XATTR_USER_PREFIX_LEN
; /* skip past user. prefix */
277 rc
= CIFSSMBQAllEAs(xid
, pTcon
, full_path
, ea_name
, ea_value
,
278 buf_size
, cifs_sb
->local_nls
,
279 cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
);
280 } else if (strncmp(ea_name
, XATTR_OS2_PREFIX
, XATTR_OS2_PREFIX_LEN
) == 0) {
281 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_XATTR
)
284 ea_name
+= XATTR_OS2_PREFIX_LEN
; /* skip past os2. prefix */
285 rc
= CIFSSMBQAllEAs(xid
, pTcon
, full_path
, ea_name
, ea_value
,
286 buf_size
, cifs_sb
->local_nls
,
287 cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
);
288 } else if (strncmp(ea_name
, POSIX_ACL_XATTR_ACCESS
,
289 strlen(POSIX_ACL_XATTR_ACCESS
)) == 0) {
290 #ifdef CONFIG_CIFS_POSIX
291 if (sb
->s_flags
& MS_POSIXACL
)
292 rc
= CIFSSMBGetPosixACL(xid
, pTcon
, full_path
,
293 ea_value
, buf_size
, ACL_TYPE_ACCESS
,
295 cifs_sb
->mnt_cifs_flags
&
296 CIFS_MOUNT_MAP_SPECIAL_CHR
);
298 cFYI(1, "Query POSIX ACL not supported yet");
299 #endif /* CONFIG_CIFS_POSIX */
300 } else if (strncmp(ea_name
, POSIX_ACL_XATTR_DEFAULT
,
301 strlen(POSIX_ACL_XATTR_DEFAULT
)) == 0) {
302 #ifdef CONFIG_CIFS_POSIX
303 if (sb
->s_flags
& MS_POSIXACL
)
304 rc
= CIFSSMBGetPosixACL(xid
, pTcon
, full_path
,
305 ea_value
, buf_size
, ACL_TYPE_DEFAULT
,
307 cifs_sb
->mnt_cifs_flags
&
308 CIFS_MOUNT_MAP_SPECIAL_CHR
);
310 cFYI(1, "Query POSIX default ACL not supported yet");
311 #endif /* CONFIG_CIFS_POSIX */
312 } else if (strncmp(ea_name
, CIFS_XATTR_CIFS_ACL
,
313 strlen(CIFS_XATTR_CIFS_ACL
)) == 0) {
314 #ifdef CONFIG_CIFS_ACL
316 struct cifs_ntsd
*pacl
;
318 pacl
= get_cifs_acl(cifs_sb
, direntry
->d_inode
,
322 cERROR(1, "%s: error %zd getting sec desc",
326 if (acllen
> buf_size
)
329 memcpy(ea_value
, pacl
, acllen
);
335 cFYI(1, "Query CIFS ACL not supported yet");
336 #endif /* CONFIG_CIFS_ACL */
337 } else if (strncmp(ea_name
,
338 XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) == 0) {
339 cFYI(1, "Trusted xattr namespace not supported yet");
340 } else if (strncmp(ea_name
,
341 XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) == 0) {
342 cFYI(1, "Security xattr namespace not supported yet");
345 "illegal xattr request %s (only user namespace supported)",
348 /* We could add an additional check for streams ie
349 if proc/fs/cifs/streamstoxattr is set then
350 search server for EAs or streams to
359 cifs_put_tlink(tlink
);
364 ssize_t
cifs_listxattr(struct dentry
*direntry
, char *data
, size_t buf_size
)
366 ssize_t rc
= -EOPNOTSUPP
;
367 #ifdef CONFIG_CIFS_XATTR
369 struct cifs_sb_info
*cifs_sb
;
370 struct tcon_link
*tlink
;
371 struct cifs_tcon
*pTcon
;
372 struct super_block
*sb
;
375 if (direntry
== NULL
)
377 if (direntry
->d_inode
== NULL
)
379 sb
= direntry
->d_inode
->i_sb
;
383 cifs_sb
= CIFS_SB(sb
);
384 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_XATTR
)
387 tlink
= cifs_sb_tlink(cifs_sb
);
389 return PTR_ERR(tlink
);
390 pTcon
= tlink_tcon(tlink
);
394 full_path
= build_path_from_dentry(direntry
);
395 if (full_path
== NULL
) {
399 /* return dos attributes as pseudo xattr */
400 /* return alt name if available as pseudo attr */
402 /* if proc/fs/cifs/streamstoxattr is set then
403 search server for EAs or streams to
405 rc
= CIFSSMBQAllEAs(xid
, pTcon
, full_path
, NULL
, data
,
406 buf_size
, cifs_sb
->local_nls
,
407 cifs_sb
->mnt_cifs_flags
&
408 CIFS_MOUNT_MAP_SPECIAL_CHR
);
413 cifs_put_tlink(tlink
);