vserver 1.9.5.x5
[linux-2.6.git] / 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                 cFYI(1,("illegal xattr namespace %s (only user namespace supported)",ea_name));
76                 /* BB what if no namespace prefix? */
77                 /* Should we just pass them to server, except for
78                 system and perhaps security prefixes? */
79         } else {
80                 ea_name+=5; /* skip past user. prefix */
81                 rc = CIFSSMBSetEA(xid,pTcon,full_path,ea_name,NULL,
82                         (__u16)0, cifs_sb->local_nls);
83         }
84         if (full_path)
85                 kfree(full_path);
86         FreeXid(xid);
87 #endif
88         return rc;
89 }
90
91 int cifs_setxattr(struct dentry * direntry, const char * ea_name,
92         const void * ea_value, size_t value_size, int flags)
93 {
94         int rc = -EOPNOTSUPP;
95 #ifdef CONFIG_CIFS_XATTR
96         int xid;
97         struct cifs_sb_info *cifs_sb;
98         struct cifsTconInfo *pTcon;
99         struct super_block * sb;
100         char * full_path;
101
102         if(direntry == NULL)
103                 return -EIO;
104         if(direntry->d_inode == NULL)
105                 return -EIO;
106         sb = direntry->d_inode->i_sb;
107         if(sb == NULL)
108                 return -EIO;
109         xid = GetXid();
110
111         cifs_sb = CIFS_SB(sb);
112         pTcon = cifs_sb->tcon;
113
114         down(&sb->s_vfs_rename_sem);
115         full_path = build_path_from_dentry(direntry);
116         up(&sb->s_vfs_rename_sem);
117         if(full_path == NULL) {
118                 FreeXid(xid);
119                 return -ENOMEM;
120         }
121         /* return dos attributes as pseudo xattr */
122         /* return alt name if available as pseudo attr */
123
124         /* if proc/fs/cifs/streamstoxattr is set then
125                 search server for EAs or streams to 
126                 returns as xattrs */
127         if(value_size > MAX_EA_VALUE_SIZE) {
128                 cFYI(1,("size of EA value too large"));
129                 if(full_path)
130                         kfree(full_path);
131                 FreeXid(xid);
132                 return -EOPNOTSUPP;
133         }
134
135         if(ea_name == NULL) {
136                 cFYI(1,("Null xattr names not supported"));
137         } else if(strncmp(ea_name,CIFS_XATTR_USER_PREFIX,5) == 0) {
138                 if(strncmp(ea_name,CIFS_XATTR_DOS_ATTRIB,14) == 0) {
139                         cFYI(1,("attempt to set cifs inode metadata"));
140                 }
141                 ea_name += 5; /* skip past user. prefix */
142                 rc = CIFSSMBSetEA(xid,pTcon,full_path,ea_name,ea_value,
143                         (__u16)value_size, cifs_sb->local_nls);
144         } else if(strncmp(ea_name, CIFS_XATTR_OS2_PREFIX,4) == 0) {
145                 ea_name += 4; /* skip past os2. prefix */
146                 rc = CIFSSMBSetEA(xid,pTcon,full_path,ea_name,ea_value,
147                         (__u16)value_size, cifs_sb->local_nls);
148         } else {
149                 int temp; 
150                 temp = strncmp(ea_name,POSIX_ACL_XATTR_ACCESS,
151                         strlen(POSIX_ACL_XATTR_ACCESS));
152                 if (temp == 0) {
153 #ifdef CONFIG_CIFS_POSIX
154                         rc = CIFSSMBSetPosixACL(xid, pTcon,full_path,ea_value,
155                                 (const int)value_size, ACL_TYPE_ACCESS,
156                                 cifs_sb->local_nls);
157                         cFYI(1,("set POSIX ACL rc %d",rc));
158 #else
159                         cFYI(1,("set POSIX ACL not supported"));
160 #endif
161                 } else if(strncmp(ea_name,POSIX_ACL_XATTR_DEFAULT,strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
162 #ifdef CONFIG_CIFS_POSIX
163                         rc = CIFSSMBSetPosixACL(xid, pTcon,full_path,ea_value,
164                                 (const int)value_size, ACL_TYPE_DEFAULT,
165                                 cifs_sb->local_nls);
166                         cFYI(1,("set POSIX default ACL rc %d",rc));
167 #else
168                         cFYI(1,("set default POSIX ACL not supported"));
169 #endif
170                 } else {
171                         cFYI(1,("illegal xattr request %s (only user namespace supported)",ea_name));
172                   /* BB what if no namespace prefix? */
173                   /* Should we just pass them to server, except for 
174                   system and perhaps security prefixes? */
175                 }
176         }
177  
178         if (full_path)
179                 kfree(full_path);
180         FreeXid(xid);
181 #endif
182         return rc;
183 }
184
185 ssize_t cifs_getxattr(struct dentry * direntry, const char * ea_name,
186          void * ea_value, size_t buf_size)
187 {
188         ssize_t rc = -EOPNOTSUPP;
189 #ifdef CONFIG_CIFS_XATTR
190         int xid;
191         struct cifs_sb_info *cifs_sb;
192         struct cifsTconInfo *pTcon;
193         struct super_block * sb;
194         char * full_path;
195
196         if(direntry == NULL)
197                 return -EIO;
198         if(direntry->d_inode == NULL)
199                 return -EIO;
200         sb = direntry->d_inode->i_sb;
201         if(sb == NULL)
202                 return -EIO;
203
204         xid = GetXid();
205
206         cifs_sb = CIFS_SB(sb);
207         pTcon = cifs_sb->tcon;
208
209         down(&sb->s_vfs_rename_sem);
210         full_path = build_path_from_dentry(direntry);
211         up(&sb->s_vfs_rename_sem);
212         if(full_path == NULL) {
213                 FreeXid(xid);
214                 return -ENOMEM;
215         }
216         /* return dos attributes as pseudo xattr */
217         /* return alt name if available as pseudo attr */
218         if(ea_name == NULL) {
219                 cFYI(1,("Null xattr names not supported"));
220         } else if(strncmp(ea_name,CIFS_XATTR_USER_PREFIX,5) == 0) {
221                 if(strncmp(ea_name,CIFS_XATTR_DOS_ATTRIB,14) == 0) {
222                         cFYI(1,("attempt to query cifs inode metadata"));
223                         /* revalidate/getattr then populate from inode */
224                 } /* BB add else when above is implemented */
225                 ea_name += 5; /* skip past user. prefix */
226                 rc = CIFSSMBQueryEA(xid,pTcon,full_path,ea_name,ea_value,
227                         buf_size, cifs_sb->local_nls);
228         } else if(strncmp(ea_name, CIFS_XATTR_OS2_PREFIX,4) == 0) {
229                 ea_name += 4; /* skip past os2. prefix */
230                 rc = CIFSSMBQueryEA(xid,pTcon,full_path,ea_name,ea_value,
231                         buf_size, cifs_sb->local_nls);
232         } else if(strncmp(ea_name,POSIX_ACL_XATTR_ACCESS,strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
233 #ifdef CONFIG_CIFS_POSIX
234                 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
235                                 ea_value, buf_size, ACL_TYPE_ACCESS, 
236                                 cifs_sb->local_nls);
237 #else 
238                 cFYI(1,("query POSIX ACL not supported yet"));
239 #endif /* CONFIG_CIFS_POSIX */
240         } else if(strncmp(ea_name,POSIX_ACL_XATTR_DEFAULT,strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
241 #ifdef CONFIG_CIFS_POSIX
242                 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
243                                 ea_value, buf_size, ACL_TYPE_DEFAULT, 
244                                 cifs_sb->local_nls);
245 #else 
246                 cFYI(1,("query POSIX default ACL not supported yet"));
247 #endif
248         } else if(strncmp(ea_name,
249                   CIFS_XATTR_TRUSTED_PREFIX,XATTR_TRUSTED_PREFIX_LEN) == 0) {
250                 cFYI(1,("Trusted xattr namespace not supported yet"));
251         } else if(strncmp(ea_name,
252                   CIFS_XATTR_SECURITY_PREFIX,XATTR_SECURITY_PREFIX_LEN) == 0) {
253                 cFYI(1,("Security xattr namespace not supported yet"));
254         } else {
255                 cFYI(1,("illegal xattr name request %s (only user namespace supported)",ea_name));
256         }
257
258         /* We could add an additional check for streams ie 
259             if proc/fs/cifs/streamstoxattr is set then
260                 search server for EAs or streams to 
261                 returns as xattrs */
262
263         if(rc == -EINVAL)
264                 rc = -EOPNOTSUPP; 
265
266         if (full_path)
267                 kfree(full_path);
268         FreeXid(xid);
269 #endif
270         return rc;
271 }
272
273 ssize_t cifs_listxattr(struct dentry * direntry, char * data, size_t buf_size)
274 {
275         ssize_t rc = -EOPNOTSUPP;
276 #ifdef CONFIG_CIFS_XATTR
277         int xid;
278         struct cifs_sb_info *cifs_sb;
279         struct cifsTconInfo *pTcon;
280         struct super_block * sb;
281         char * full_path;
282
283         if(direntry == NULL)
284                 return -EIO;
285         if(direntry->d_inode == NULL)
286                 return -EIO;
287         sb = direntry->d_inode->i_sb;
288         if(sb == NULL)
289                 return -EIO;
290         xid = GetXid();
291
292         cifs_sb = CIFS_SB(sb);
293         pTcon = cifs_sb->tcon;
294
295         down(&sb->s_vfs_rename_sem);
296         full_path = build_path_from_dentry(direntry);
297         up(&sb->s_vfs_rename_sem);
298         if(full_path == NULL) {
299                 FreeXid(xid);
300                 return -ENOMEM;
301         }
302         /* return dos attributes as pseudo xattr */
303         /* return alt name if available as pseudo attr */
304
305         /* if proc/fs/cifs/streamstoxattr is set then
306                 search server for EAs or streams to 
307                 returns as xattrs */
308         rc = CIFSSMBQAllEAs(xid,pTcon,full_path,data,buf_size,
309                                 cifs_sb->local_nls);
310
311         if (full_path)
312                 kfree(full_path);
313         FreeXid(xid);
314 #endif
315         return rc;
316 }