Merge branch 'master'
[linux-2.6] / fs / cifs / xattr.c
1 /*
2  *   fs/cifs/xattr.c
3  *
4  *   Copyright (c) International Business Machines  Corp., 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
22 #include <linux/fs.h>
23 #include <linux/posix_acl_xattr.h>
24 #include "cifsfs.h"
25 #include "cifspdu.h"
26 #include "cifsglob.h"
27 #include "cifsproto.h"
28 #include "cifs_debug.h"
29
30 #define MAX_EA_VALUE_SIZE 65535
31 #define CIFS_XATTR_DOS_ATTRIB "user.DosAttrib"
32 #define CIFS_XATTR_USER_PREFIX "user."
33 #define CIFS_XATTR_SYSTEM_PREFIX "system."
34 #define CIFS_XATTR_OS2_PREFIX "os2."
35 #define CIFS_XATTR_SECURITY_PREFIX ".security"
36 #define CIFS_XATTR_TRUSTED_PREFIX "trusted."
37 #define XATTR_TRUSTED_PREFIX_LEN  8
38 #define XATTR_SECURITY_PREFIX_LEN 9
39 /* BB need to add server (Samba e.g) support for security and trusted prefix */
40   
41
42
43 int cifs_removexattr(struct dentry * direntry, const char * ea_name)
44 {
45         int rc = -EOPNOTSUPP;
46 #ifdef CONFIG_CIFS_XATTR
47         int xid;
48         struct cifs_sb_info *cifs_sb;
49         struct cifsTconInfo *pTcon;
50         struct super_block * sb;
51         char * full_path;
52                                                                                      
53         if(direntry == NULL)
54                 return -EIO;
55         if(direntry->d_inode == NULL)
56                 return -EIO;
57         sb = direntry->d_inode->i_sb;
58         if(sb == NULL)
59                 return -EIO;
60         xid = GetXid();
61                                                                                      
62         cifs_sb = CIFS_SB(sb);
63         pTcon = cifs_sb->tcon;
64                                                                                      
65         down(&sb->s_vfs_rename_sem);
66         full_path = build_path_from_dentry(direntry);
67         up(&sb->s_vfs_rename_sem);
68         if(full_path == NULL) {
69                 FreeXid(xid);
70                 return -ENOMEM;
71         }
72         if(ea_name == NULL) {
73                 cFYI(1,("Null xattr names not supported"));
74         } else if(strncmp(ea_name,CIFS_XATTR_USER_PREFIX,5)
75                 && (strncmp(ea_name,CIFS_XATTR_OS2_PREFIX,4))) {
76                 cFYI(1,("illegal xattr namespace %s (only user namespace supported)",ea_name));
77                 /* BB what if no namespace prefix? */
78                 /* Should we just pass them to server, except for
79                 system and perhaps security prefixes? */
80         } else {
81                 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
82                         goto remove_ea_exit;
83
84                 ea_name+=5; /* 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);
88         }
89 remove_ea_exit:
90         if (full_path)
91                 kfree(full_path);
92         FreeXid(xid);
93 #endif
94         return rc;
95 }
96
97 int cifs_setxattr(struct dentry * direntry, const char * ea_name,
98         const void * ea_value, size_t value_size, int flags)
99 {
100         int rc = -EOPNOTSUPP;
101 #ifdef CONFIG_CIFS_XATTR
102         int xid;
103         struct cifs_sb_info *cifs_sb;
104         struct cifsTconInfo *pTcon;
105         struct super_block * sb;
106         char * full_path;
107
108         if(direntry == NULL)
109                 return -EIO;
110         if(direntry->d_inode == NULL)
111                 return -EIO;
112         sb = direntry->d_inode->i_sb;
113         if(sb == NULL)
114                 return -EIO;
115         xid = GetXid();
116
117         cifs_sb = CIFS_SB(sb);
118         pTcon = cifs_sb->tcon;
119
120         down(&sb->s_vfs_rename_sem);
121         full_path = build_path_from_dentry(direntry);
122         up(&sb->s_vfs_rename_sem);
123         if(full_path == NULL) {
124                 FreeXid(xid);
125                 return -ENOMEM;
126         }
127         /* return dos attributes as pseudo xattr */
128         /* return alt name if available as pseudo attr */
129
130         /* if proc/fs/cifs/streamstoxattr is set then
131                 search server for EAs or streams to 
132                 returns as xattrs */
133         if(value_size > MAX_EA_VALUE_SIZE) {
134                 cFYI(1,("size of EA value too large"));
135                 if(full_path)
136                         kfree(full_path);
137                 FreeXid(xid);
138                 return -EOPNOTSUPP;
139         }
140
141         if(ea_name == NULL) {
142                 cFYI(1,("Null xattr names not supported"));
143         } else if(strncmp(ea_name,CIFS_XATTR_USER_PREFIX,5) == 0) {
144                 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
145                         goto set_ea_exit;
146                 if(strncmp(ea_name,CIFS_XATTR_DOS_ATTRIB,14) == 0) {
147                         cFYI(1,("attempt to set cifs inode metadata"));
148                 }
149                 ea_name += 5; /* skip past user. prefix */
150                 rc = CIFSSMBSetEA(xid,pTcon,full_path,ea_name,ea_value,
151                         (__u16)value_size, cifs_sb->local_nls,
152                         cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
153         } else if(strncmp(ea_name, CIFS_XATTR_OS2_PREFIX,4) == 0) {
154                 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
155                         goto set_ea_exit;
156
157                 ea_name += 4; /* skip past os2. prefix */
158                 rc = CIFSSMBSetEA(xid,pTcon,full_path,ea_name,ea_value,
159                         (__u16)value_size, cifs_sb->local_nls,
160                         cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
161         } else {
162                 int temp; 
163                 temp = strncmp(ea_name,POSIX_ACL_XATTR_ACCESS,
164                         strlen(POSIX_ACL_XATTR_ACCESS));
165                 if (temp == 0) {
166 #ifdef CONFIG_CIFS_POSIX
167                         if(sb->s_flags & MS_POSIXACL)
168                                 rc = CIFSSMBSetPosixACL(xid, pTcon,full_path,
169                                         ea_value, (const int)value_size, 
170                                         ACL_TYPE_ACCESS,cifs_sb->local_nls,
171                                         cifs_sb->mnt_cifs_flags & 
172                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
173                         cFYI(1,("set POSIX ACL rc %d",rc));
174 #else
175                         cFYI(1,("set POSIX ACL not supported"));
176 #endif
177                 } else if(strncmp(ea_name,POSIX_ACL_XATTR_DEFAULT,strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
178 #ifdef CONFIG_CIFS_POSIX
179                         if(sb->s_flags & MS_POSIXACL)
180                                 rc = CIFSSMBSetPosixACL(xid, pTcon,full_path,
181                                         ea_value, (const int)value_size, 
182                                         ACL_TYPE_DEFAULT, cifs_sb->local_nls,
183                                         cifs_sb->mnt_cifs_flags & 
184                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
185                         cFYI(1,("set POSIX default ACL rc %d",rc));
186 #else
187                         cFYI(1,("set default POSIX ACL not supported"));
188 #endif
189                 } else {
190                         cFYI(1,("illegal xattr request %s (only user namespace supported)",ea_name));
191                   /* BB what if no namespace prefix? */
192                   /* Should we just pass them to server, except for 
193                   system and perhaps security prefixes? */
194                 }
195         }
196
197 set_ea_exit:
198         if (full_path)
199                 kfree(full_path);
200         FreeXid(xid);
201 #endif
202         return rc;
203 }
204
205 ssize_t cifs_getxattr(struct dentry * direntry, const char * ea_name,
206          void * ea_value, size_t buf_size)
207 {
208         ssize_t rc = -EOPNOTSUPP;
209 #ifdef CONFIG_CIFS_XATTR
210         int xid;
211         struct cifs_sb_info *cifs_sb;
212         struct cifsTconInfo *pTcon;
213         struct super_block * sb;
214         char * full_path;
215
216         if(direntry == NULL)
217                 return -EIO;
218         if(direntry->d_inode == NULL)
219                 return -EIO;
220         sb = direntry->d_inode->i_sb;
221         if(sb == NULL)
222                 return -EIO;
223
224         xid = GetXid();
225
226         cifs_sb = CIFS_SB(sb);
227         pTcon = cifs_sb->tcon;
228
229         down(&sb->s_vfs_rename_sem);
230         full_path = build_path_from_dentry(direntry);
231         up(&sb->s_vfs_rename_sem);
232         if(full_path == NULL) {
233                 FreeXid(xid);
234                 return -ENOMEM;
235         }
236         /* return dos attributes as pseudo xattr */
237         /* return alt name if available as pseudo attr */
238         if(ea_name == NULL) {
239                 cFYI(1,("Null xattr names not supported"));
240         } else if(strncmp(ea_name,CIFS_XATTR_USER_PREFIX,5) == 0) {
241                 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
242                         goto get_ea_exit;
243
244                 if(strncmp(ea_name,CIFS_XATTR_DOS_ATTRIB,14) == 0) {
245                         cFYI(1,("attempt to query cifs inode metadata"));
246                         /* revalidate/getattr then populate from inode */
247                 } /* BB add else when above is implemented */
248                 ea_name += 5; /* skip past user. prefix */
249                 rc = CIFSSMBQueryEA(xid,pTcon,full_path,ea_name,ea_value,
250                         buf_size, cifs_sb->local_nls,
251                         cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
252         } else if(strncmp(ea_name, CIFS_XATTR_OS2_PREFIX,4) == 0) {
253                 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
254                         goto get_ea_exit;
255
256                 ea_name += 4; /* skip past os2. prefix */
257                 rc = CIFSSMBQueryEA(xid,pTcon,full_path,ea_name,ea_value,
258                         buf_size, cifs_sb->local_nls,
259                         cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
260         } else if(strncmp(ea_name,POSIX_ACL_XATTR_ACCESS,strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
261 #ifdef CONFIG_CIFS_POSIX
262                 if(sb->s_flags & MS_POSIXACL)
263                         rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
264                                 ea_value, buf_size, ACL_TYPE_ACCESS, 
265                                 cifs_sb->local_nls,
266                                 cifs_sb->mnt_cifs_flags & 
267                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
268 #else 
269                 cFYI(1,("query POSIX ACL not supported yet"));
270 #endif /* CONFIG_CIFS_POSIX */
271         } else if(strncmp(ea_name,POSIX_ACL_XATTR_DEFAULT,strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
272 #ifdef CONFIG_CIFS_POSIX
273                 if(sb->s_flags & MS_POSIXACL)
274                         rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
275                                 ea_value, buf_size, ACL_TYPE_DEFAULT, 
276                                 cifs_sb->local_nls,
277                                 cifs_sb->mnt_cifs_flags & 
278                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
279 #else 
280                 cFYI(1,("query POSIX default ACL not supported yet"));
281 #endif
282         } else if(strncmp(ea_name,
283                   CIFS_XATTR_TRUSTED_PREFIX,XATTR_TRUSTED_PREFIX_LEN) == 0) {
284                 cFYI(1,("Trusted xattr namespace not supported yet"));
285         } else if(strncmp(ea_name,
286                   CIFS_XATTR_SECURITY_PREFIX,XATTR_SECURITY_PREFIX_LEN) == 0) {
287                 cFYI(1,("Security xattr namespace not supported yet"));
288         } else {
289                 cFYI(1,("illegal xattr name request %s (only user namespace supported)",ea_name));
290         }
291
292         /* We could add an additional check for streams ie 
293             if proc/fs/cifs/streamstoxattr is set then
294                 search server for EAs or streams to 
295                 returns as xattrs */
296
297         if(rc == -EINVAL)
298                 rc = -EOPNOTSUPP; 
299
300 get_ea_exit:
301         if (full_path)
302                 kfree(full_path);
303         FreeXid(xid);
304 #endif
305         return rc;
306 }
307
308 ssize_t cifs_listxattr(struct dentry * direntry, char * data, size_t buf_size)
309 {
310         ssize_t rc = -EOPNOTSUPP;
311 #ifdef CONFIG_CIFS_XATTR
312         int xid;
313         struct cifs_sb_info *cifs_sb;
314         struct cifsTconInfo *pTcon;
315         struct super_block * sb;
316         char * full_path;
317
318         if(direntry == NULL)
319                 return -EIO;
320         if(direntry->d_inode == NULL)
321                 return -EIO;
322         sb = direntry->d_inode->i_sb;
323         if(sb == NULL)
324                 return -EIO;
325         xid = GetXid();
326
327         cifs_sb = CIFS_SB(sb);
328         pTcon = cifs_sb->tcon;
329
330         down(&sb->s_vfs_rename_sem);
331         full_path = build_path_from_dentry(direntry);
332         up(&sb->s_vfs_rename_sem);
333         if(full_path == NULL) {
334                 FreeXid(xid);
335                 return -ENOMEM;
336         }
337         /* return dos attributes as pseudo xattr */
338         /* return alt name if available as pseudo attr */
339
340         /* if proc/fs/cifs/streamstoxattr is set then
341                 search server for EAs or streams to 
342                 returns as xattrs */
343         rc = CIFSSMBQAllEAs(xid,pTcon,full_path,data,buf_size,
344                                 cifs_sb->local_nls,
345                                 cifs_sb->mnt_cifs_flags & 
346                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
347
348         if (full_path)
349                 kfree(full_path);
350         FreeXid(xid);
351 #endif
352         return rc;
353 }