Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/cifs/dir.c | |
3 | * | |
4 | * vfs operations that deal with dentries | |
5 | * | |
6 | * Copyright (C) International Business Machines Corp., 2002,2003 | |
7 | * Author(s): Steve French (sfrench@us.ibm.com) | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU Lesser General Public License as published | |
11 | * by the Free Software Foundation; either version 2.1 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | |
17 | * the GNU Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public License | |
20 | * along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | */ | |
23 | #include <linux/fs.h> | |
24 | #include <linux/stat.h> | |
25 | #include <linux/slab.h> | |
26 | #include <linux/namei.h> | |
27 | #include "cifsfs.h" | |
28 | #include "cifspdu.h" | |
29 | #include "cifsglob.h" | |
30 | #include "cifsproto.h" | |
31 | #include "cifs_debug.h" | |
32 | #include "cifs_fs_sb.h" | |
33 | ||
34 | void | |
35 | renew_parental_timestamps(struct dentry *direntry) | |
36 | { | |
37 | /* BB check if there is a way to get the kernel to do this or if we really need this */ | |
38 | do { | |
39 | direntry->d_time = jiffies; | |
40 | direntry = direntry->d_parent; | |
41 | } while (!IS_ROOT(direntry)); | |
42 | } | |
43 | ||
44 | /* Note: caller must free return buffer */ | |
45 | char * | |
46 | build_path_from_dentry(struct dentry *direntry) | |
47 | { | |
48 | struct dentry *temp; | |
49 | int namelen = 0; | |
50 | char *full_path; | |
51 | ||
52 | if(direntry == NULL) | |
53 | return NULL; /* not much we can do if dentry is freed and | |
54 | we need to reopen the file after it was closed implicitly | |
55 | when the server crashed */ | |
56 | ||
57 | cifs_bp_rename_retry: | |
58 | for (temp = direntry; !IS_ROOT(temp);) { | |
59 | namelen += (1 + temp->d_name.len); | |
60 | temp = temp->d_parent; | |
61 | if(temp == NULL) { | |
62 | cERROR(1,("corrupt dentry")); | |
63 | return NULL; | |
64 | } | |
65 | } | |
66 | ||
67 | full_path = kmalloc(namelen+1, GFP_KERNEL); | |
68 | if(full_path == NULL) | |
69 | return full_path; | |
70 | full_path[namelen] = 0; /* trailing null */ | |
71 | ||
72 | for (temp = direntry; !IS_ROOT(temp);) { | |
73 | namelen -= 1 + temp->d_name.len; | |
74 | if (namelen < 0) { | |
75 | break; | |
76 | } else { | |
77 | full_path[namelen] = '\\'; | |
78 | strncpy(full_path + namelen + 1, temp->d_name.name, | |
79 | temp->d_name.len); | |
80 | cFYI(0, (" name: %s ", full_path + namelen)); | |
81 | } | |
82 | temp = temp->d_parent; | |
83 | if(temp == NULL) { | |
84 | cERROR(1,("corrupt dentry")); | |
85 | kfree(full_path); | |
86 | return NULL; | |
87 | } | |
88 | } | |
89 | if (namelen != 0) { | |
90 | cERROR(1, | |
91 | ("We did not end path lookup where we expected namelen is %d", | |
92 | namelen)); | |
93 | /* presumably this is only possible if we were racing with a rename | |
94 | of one of the parent directories (we can not lock the dentries | |
95 | above us to prevent this, but retrying should be harmless) */ | |
96 | kfree(full_path); | |
97 | namelen = 0; | |
98 | goto cifs_bp_rename_retry; | |
99 | } | |
100 | ||
101 | return full_path; | |
102 | } | |
103 | ||
737b758c | 104 | /* char * build_wildcard_path_from_dentry(struct dentry *direntry) |
1da177e4 | 105 | { |
1da177e4 LT |
106 | if(full_path == NULL) |
107 | return full_path; | |
108 | ||
109 | full_path[namelen] = '\\'; | |
110 | full_path[namelen+1] = '*'; | |
737b758c SF |
111 | full_path[namelen+2] = 0; |
112 | BB remove above eight lines BB */ | |
1da177e4 LT |
113 | |
114 | /* Inode operations in similar order to how they appear in the Linux file fs.h */ | |
115 | ||
116 | int | |
117 | cifs_create(struct inode *inode, struct dentry *direntry, int mode, | |
118 | struct nameidata *nd) | |
119 | { | |
120 | int rc = -ENOENT; | |
121 | int xid; | |
122 | int oplock = 0; | |
123 | int desiredAccess = GENERIC_READ | GENERIC_WRITE; | |
124 | __u16 fileHandle; | |
125 | struct cifs_sb_info *cifs_sb; | |
126 | struct cifsTconInfo *pTcon; | |
127 | char *full_path = NULL; | |
128 | FILE_ALL_INFO * buf = NULL; | |
129 | struct inode *newinode = NULL; | |
130 | struct cifsFileInfo * pCifsFile = NULL; | |
131 | struct cifsInodeInfo * pCifsInode; | |
132 | int disposition = FILE_OVERWRITE_IF; | |
133 | int write_only = FALSE; | |
134 | ||
135 | xid = GetXid(); | |
136 | ||
137 | cifs_sb = CIFS_SB(inode->i_sb); | |
138 | pTcon = cifs_sb->tcon; | |
139 | ||
140 | down(&direntry->d_sb->s_vfs_rename_sem); | |
141 | full_path = build_path_from_dentry(direntry); | |
142 | up(&direntry->d_sb->s_vfs_rename_sem); | |
143 | if(full_path == NULL) { | |
144 | FreeXid(xid); | |
145 | return -ENOMEM; | |
146 | } | |
147 | ||
e08fc045 MS |
148 | if(nd && (nd->flags & LOOKUP_OPEN)) { |
149 | int oflags = nd->intent.open.flags; | |
150 | ||
151 | desiredAccess = 0; | |
152 | if (oflags & FMODE_READ) | |
153 | desiredAccess |= GENERIC_READ; | |
154 | if (oflags & FMODE_WRITE) { | |
155 | desiredAccess |= GENERIC_WRITE; | |
156 | if (!(oflags & FMODE_READ)) | |
157 | write_only = TRUE; | |
1da177e4 LT |
158 | } |
159 | ||
e08fc045 | 160 | if((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
1da177e4 | 161 | disposition = FILE_CREATE; |
e08fc045 | 162 | else if((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) |
1da177e4 | 163 | disposition = FILE_OVERWRITE_IF; |
e08fc045 | 164 | else if((oflags & O_CREAT) == O_CREAT) |
1da177e4 LT |
165 | disposition = FILE_OPEN_IF; |
166 | else { | |
167 | cFYI(1,("Create flag not set in create function")); | |
168 | } | |
169 | } | |
170 | ||
171 | /* BB add processing to set equivalent of mode - e.g. via CreateX with ACLs */ | |
172 | if (oplockEnabled) | |
173 | oplock = REQ_OPLOCK; | |
174 | ||
175 | buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL); | |
176 | if(buf == NULL) { | |
177 | kfree(full_path); | |
178 | FreeXid(xid); | |
179 | return -ENOMEM; | |
180 | } | |
181 | ||
182 | rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, | |
183 | desiredAccess, CREATE_NOT_DIR, | |
737b758c SF |
184 | &fileHandle, &oplock, buf, cifs_sb->local_nls, |
185 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
186 | if (rc) { |
187 | cFYI(1, ("cifs_create returned 0x%x ", rc)); | |
188 | } else { | |
189 | /* If Open reported that we actually created a file | |
190 | then we now have to set the mode if possible */ | |
191 | if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX) && | |
192 | (oplock & CIFS_CREATE_ACTION)) | |
193 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { | |
194 | CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, | |
195 | (__u64)current->euid, | |
196 | (__u64)current->egid, | |
197 | 0 /* dev */, | |
737b758c SF |
198 | cifs_sb->local_nls, |
199 | cifs_sb->mnt_cifs_flags & | |
200 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
201 | } else { |
202 | CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, | |
203 | (__u64)-1, | |
204 | (__u64)-1, | |
205 | 0 /* dev */, | |
737b758c SF |
206 | cifs_sb->local_nls, |
207 | cifs_sb->mnt_cifs_flags & | |
208 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
209 | } |
210 | else { | |
211 | /* BB implement via Windows security descriptors */ | |
212 | /* eg CIFSSMBWinSetPerms(xid,pTcon,full_path,mode,-1,-1,local_nls);*/ | |
213 | /* could set r/o dos attribute if mode & 0222 == 0 */ | |
214 | } | |
215 | ||
216 | /* BB server might mask mode so we have to query for Unix case*/ | |
217 | if (pTcon->ses->capabilities & CAP_UNIX) | |
218 | rc = cifs_get_inode_info_unix(&newinode, full_path, | |
219 | inode->i_sb,xid); | |
220 | else { | |
221 | rc = cifs_get_inode_info(&newinode, full_path, | |
222 | buf, inode->i_sb,xid); | |
223 | if(newinode) | |
224 | newinode->i_mode = mode; | |
225 | } | |
226 | ||
227 | if (rc != 0) { | |
228 | cFYI(1,("Create worked but get_inode_info failed with rc = %d", | |
229 | rc)); | |
230 | } else { | |
231 | direntry->d_op = &cifs_dentry_ops; | |
232 | d_instantiate(direntry, newinode); | |
233 | } | |
234 | if((nd->flags & LOOKUP_OPEN) == FALSE) { | |
235 | /* mknod case - do not leave file open */ | |
236 | CIFSSMBClose(xid, pTcon, fileHandle); | |
237 | } else if(newinode) { | |
d14537f1 | 238 | pCifsFile = |
1da177e4 | 239 | kmalloc(sizeof (struct cifsFileInfo), GFP_KERNEL); |
d14537f1 SF |
240 | |
241 | if(pCifsFile == NULL) | |
242 | goto cifs_create_out; | |
243 | memset((char *)pCifsFile, 0, | |
244 | sizeof (struct cifsFileInfo)); | |
245 | pCifsFile->netfid = fileHandle; | |
246 | pCifsFile->pid = current->tgid; | |
247 | pCifsFile->pInode = newinode; | |
248 | pCifsFile->invalidHandle = FALSE; | |
249 | pCifsFile->closePend = FALSE; | |
250 | init_MUTEX(&pCifsFile->fh_sem); | |
251 | /* set the following in open now | |
252 | pCifsFile->pfile = file; */ | |
253 | write_lock(&GlobalSMBSeslock); | |
254 | list_add(&pCifsFile->tlist,&pTcon->openFileList); | |
255 | pCifsInode = CIFS_I(newinode); | |
256 | if(pCifsInode) { | |
1da177e4 | 257 | /* if readable file instance put first in list*/ |
d14537f1 SF |
258 | if (write_only == TRUE) { |
259 | list_add_tail(&pCifsFile->flist, | |
260 | &pCifsInode->openFileList); | |
261 | } else { | |
262 | list_add(&pCifsFile->flist, | |
263 | &pCifsInode->openFileList); | |
1da177e4 | 264 | } |
d14537f1 SF |
265 | if((oplock & 0xF) == OPLOCK_EXCLUSIVE) { |
266 | pCifsInode->clientCanCacheAll = TRUE; | |
267 | pCifsInode->clientCanCacheRead = TRUE; | |
268 | cFYI(1,("Exclusive Oplock for inode %p", | |
269 | newinode)); | |
270 | } else if((oplock & 0xF) == OPLOCK_READ) | |
271 | pCifsInode->clientCanCacheRead = TRUE; | |
1da177e4 | 272 | } |
d14537f1 | 273 | write_unlock(&GlobalSMBSeslock); |
1da177e4 LT |
274 | } |
275 | } | |
d14537f1 SF |
276 | cifs_create_out: |
277 | kfree(buf); | |
278 | kfree(full_path); | |
1da177e4 | 279 | FreeXid(xid); |
1da177e4 LT |
280 | return rc; |
281 | } | |
282 | ||
283 | int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, dev_t device_number) | |
284 | { | |
285 | int rc = -EPERM; | |
286 | int xid; | |
287 | struct cifs_sb_info *cifs_sb; | |
288 | struct cifsTconInfo *pTcon; | |
289 | char *full_path = NULL; | |
290 | struct inode * newinode = NULL; | |
291 | ||
292 | if (!old_valid_dev(device_number)) | |
293 | return -EINVAL; | |
294 | ||
295 | xid = GetXid(); | |
296 | ||
297 | cifs_sb = CIFS_SB(inode->i_sb); | |
298 | pTcon = cifs_sb->tcon; | |
299 | ||
300 | down(&direntry->d_sb->s_vfs_rename_sem); | |
301 | full_path = build_path_from_dentry(direntry); | |
302 | up(&direntry->d_sb->s_vfs_rename_sem); | |
303 | if(full_path == NULL) | |
304 | rc = -ENOMEM; | |
305 | ||
306 | if (full_path && (pTcon->ses->capabilities & CAP_UNIX)) { | |
307 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { | |
308 | rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, | |
309 | mode,(__u64)current->euid,(__u64)current->egid, | |
737b758c SF |
310 | device_number, cifs_sb->local_nls, |
311 | cifs_sb->mnt_cifs_flags & | |
312 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
313 | } else { |
314 | rc = CIFSSMBUnixSetPerms(xid, pTcon, | |
315 | full_path, mode, (__u64)-1, (__u64)-1, | |
737b758c SF |
316 | device_number, cifs_sb->local_nls, |
317 | cifs_sb->mnt_cifs_flags & | |
318 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
319 | } |
320 | ||
321 | if(!rc) { | |
322 | rc = cifs_get_inode_info_unix(&newinode, full_path, | |
323 | inode->i_sb,xid); | |
324 | direntry->d_op = &cifs_dentry_ops; | |
325 | if(rc == 0) | |
326 | d_instantiate(direntry, newinode); | |
327 | } | |
328 | } | |
329 | ||
d14537f1 | 330 | kfree(full_path); |
1da177e4 | 331 | FreeXid(xid); |
1da177e4 LT |
332 | return rc; |
333 | } | |
334 | ||
335 | ||
336 | struct dentry * | |
337 | cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd) | |
338 | { | |
339 | int xid; | |
340 | int rc = 0; /* to get around spurious gcc warning, set to zero here */ | |
341 | struct cifs_sb_info *cifs_sb; | |
342 | struct cifsTconInfo *pTcon; | |
343 | struct inode *newInode = NULL; | |
344 | char *full_path = NULL; | |
345 | ||
346 | xid = GetXid(); | |
347 | ||
348 | cFYI(1, | |
349 | (" parent inode = 0x%p name is: %s and dentry = 0x%p", | |
350 | parent_dir_inode, direntry->d_name.name, direntry)); | |
351 | ||
352 | /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */ | |
353 | ||
354 | /* check whether path exists */ | |
355 | ||
356 | cifs_sb = CIFS_SB(parent_dir_inode->i_sb); | |
357 | pTcon = cifs_sb->tcon; | |
358 | ||
359 | /* can not grab the rename sem here since it would | |
360 | deadlock in the cases (beginning of sys_rename itself) | |
361 | in which we already have the sb rename sem */ | |
362 | full_path = build_path_from_dentry(direntry); | |
363 | if(full_path == NULL) { | |
364 | FreeXid(xid); | |
365 | return ERR_PTR(-ENOMEM); | |
366 | } | |
367 | ||
368 | if (direntry->d_inode != NULL) { | |
369 | cFYI(1, (" non-NULL inode in lookup")); | |
370 | } else { | |
371 | cFYI(1, (" NULL inode in lookup")); | |
372 | } | |
373 | cFYI(1, | |
374 | (" Full path: %s inode = 0x%p", full_path, direntry->d_inode)); | |
375 | ||
376 | if (pTcon->ses->capabilities & CAP_UNIX) | |
377 | rc = cifs_get_inode_info_unix(&newInode, full_path, | |
378 | parent_dir_inode->i_sb,xid); | |
379 | else | |
380 | rc = cifs_get_inode_info(&newInode, full_path, NULL, | |
381 | parent_dir_inode->i_sb,xid); | |
382 | ||
383 | if ((rc == 0) && (newInode != NULL)) { | |
384 | direntry->d_op = &cifs_dentry_ops; | |
385 | d_add(direntry, newInode); | |
386 | ||
387 | /* since paths are not looked up by component - the parent directories are presumed to be good here */ | |
388 | renew_parental_timestamps(direntry); | |
389 | ||
390 | } else if (rc == -ENOENT) { | |
391 | rc = 0; | |
392 | d_add(direntry, NULL); | |
393 | } else { | |
b2aeb9d5 SF |
394 | cERROR(1,("Error 0x%x on cifs_get_inode_info in lookup of %s", |
395 | rc,full_path)); | |
1da177e4 LT |
396 | /* BB special case check for Access Denied - watch security |
397 | exposure of returning dir info implicitly via different rc | |
398 | if file exists or not but no access BB */ | |
399 | } | |
400 | ||
d14537f1 | 401 | kfree(full_path); |
1da177e4 LT |
402 | FreeXid(xid); |
403 | return ERR_PTR(rc); | |
404 | } | |
405 | ||
1da177e4 LT |
406 | static int |
407 | cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd) | |
408 | { | |
409 | int isValid = 1; | |
410 | ||
411 | /* lock_kernel(); *//* surely we do not want to lock the kernel for a whole network round trip which could take seconds */ | |
412 | ||
413 | if (direntry->d_inode) { | |
414 | if (cifs_revalidate(direntry)) { | |
415 | /* unlock_kernel(); */ | |
416 | return 0; | |
417 | } | |
418 | } else { | |
419 | cFYI(1, | |
420 | ("In cifs_d_revalidate with no inode but name = %s and dentry 0x%p", | |
421 | direntry->d_name.name, direntry)); | |
422 | } | |
423 | ||
424 | /* unlock_kernel(); */ | |
425 | ||
426 | return isValid; | |
427 | } | |
428 | ||
429 | /* static int cifs_d_delete(struct dentry *direntry) | |
430 | { | |
431 | int rc = 0; | |
432 | ||
433 | cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name)); | |
434 | ||
435 | return rc; | |
436 | } */ | |
437 | ||
438 | struct dentry_operations cifs_dentry_ops = { | |
439 | .d_revalidate = cifs_d_revalidate, | |
440 | /* d_delete: cifs_d_delete, *//* not needed except for debugging */ | |
441 | /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */ | |
442 | }; |