]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/cifs/dir.c
[CIFS] Make CIFS statistics more accurate and add some stats that were
[mirror_ubuntu-artful-kernel.git] / fs / cifs / dir.c
CommitLineData
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
34void
35renew_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 */
45char *
ac67055e 46build_path_from_dentry(struct dentry *direntry, const struct cifs_sb_info *cifs_sb)
1da177e4
LT
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
57cifs_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 {
ac67055e 77 full_path[namelen] = CIFS_DIR_SEP(cifs_sb);
1da177e4
LT
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;
112BB 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
116int
117cifs_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);
ac67055e 141 full_path = build_path_from_dentry(direntry, cifs_sb);
1da177e4
LT
142 up(&direntry->d_sb->s_vfs_rename_sem);
143 if(full_path == NULL) {
144 FreeXid(xid);
145 return -ENOMEM;
146 }
147
148 if(nd) {
149 if ((nd->intent.open.flags & O_ACCMODE) == O_RDONLY)
150 desiredAccess = GENERIC_READ;
151 else if ((nd->intent.open.flags & O_ACCMODE) == O_WRONLY) {
152 desiredAccess = GENERIC_WRITE;
153 write_only = TRUE;
154 } else if ((nd->intent.open.flags & O_ACCMODE) == O_RDWR) {
155 /* GENERIC_ALL is too much permission to request */
156 /* can cause unnecessary access denied on create */
157 /* desiredAccess = GENERIC_ALL; */
158 desiredAccess = GENERIC_READ | GENERIC_WRITE;
159 }
160
161 if((nd->intent.open.flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
162 disposition = FILE_CREATE;
163 else if((nd->intent.open.flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
164 disposition = FILE_OVERWRITE_IF;
165 else if((nd->intent.open.flags & O_CREAT) == O_CREAT)
166 disposition = FILE_OPEN_IF;
167 else {
168 cFYI(1,("Create flag not set in create function"));
169 }
170 }
171
172 /* BB add processing to set equivalent of mode - e.g. via CreateX with ACLs */
173 if (oplockEnabled)
174 oplock = REQ_OPLOCK;
175
176 buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);
177 if(buf == NULL) {
178 kfree(full_path);
179 FreeXid(xid);
180 return -ENOMEM;
181 }
182
183 rc = CIFSSMBOpen(xid, pTcon, full_path, disposition,
184 desiredAccess, CREATE_NOT_DIR,
737b758c
SF
185 &fileHandle, &oplock, buf, cifs_sb->local_nls,
186 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
187 if (rc) {
188 cFYI(1, ("cifs_create returned 0x%x ", rc));
189 } else {
190 /* If Open reported that we actually created a file
191 then we now have to set the mode if possible */
192 if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX) &&
193 (oplock & CIFS_CREATE_ACTION))
194 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
195 CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode,
196 (__u64)current->euid,
197 (__u64)current->egid,
198 0 /* dev */,
737b758c
SF
199 cifs_sb->local_nls,
200 cifs_sb->mnt_cifs_flags &
201 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
202 } else {
203 CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode,
204 (__u64)-1,
205 (__u64)-1,
206 0 /* dev */,
737b758c
SF
207 cifs_sb->local_nls,
208 cifs_sb->mnt_cifs_flags &
209 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
210 }
211 else {
d7245c2c 212 /* BB implement mode setting via Windows security descriptors */
1da177e4
LT
213 /* eg CIFSSMBWinSetPerms(xid,pTcon,full_path,mode,-1,-1,local_nls);*/
214 /* could set r/o dos attribute if mode & 0222 == 0 */
215 }
216
217 /* BB server might mask mode so we have to query for Unix case*/
218 if (pTcon->ses->capabilities & CAP_UNIX)
219 rc = cifs_get_inode_info_unix(&newinode, full_path,
220 inode->i_sb,xid);
221 else {
222 rc = cifs_get_inode_info(&newinode, full_path,
223 buf, inode->i_sb,xid);
224 if(newinode)
225 newinode->i_mode = mode;
226 }
227
228 if (rc != 0) {
4a6d87f1
SF
229 cFYI(1,
230 ("Create worked but get_inode_info failed rc = %d",
1da177e4
LT
231 rc));
232 } else {
233 direntry->d_op = &cifs_dentry_ops;
234 d_instantiate(direntry, newinode);
235 }
236 if((nd->flags & LOOKUP_OPEN) == FALSE) {
237 /* mknod case - do not leave file open */
238 CIFSSMBClose(xid, pTcon, fileHandle);
239 } else if(newinode) {
d14537f1 240 pCifsFile =
1da177e4 241 kmalloc(sizeof (struct cifsFileInfo), GFP_KERNEL);
d14537f1
SF
242
243 if(pCifsFile == NULL)
244 goto cifs_create_out;
245 memset((char *)pCifsFile, 0,
246 sizeof (struct cifsFileInfo));
247 pCifsFile->netfid = fileHandle;
248 pCifsFile->pid = current->tgid;
249 pCifsFile->pInode = newinode;
250 pCifsFile->invalidHandle = FALSE;
251 pCifsFile->closePend = FALSE;
252 init_MUTEX(&pCifsFile->fh_sem);
253 /* set the following in open now
254 pCifsFile->pfile = file; */
255 write_lock(&GlobalSMBSeslock);
256 list_add(&pCifsFile->tlist,&pTcon->openFileList);
257 pCifsInode = CIFS_I(newinode);
258 if(pCifsInode) {
1da177e4 259 /* if readable file instance put first in list*/
d14537f1
SF
260 if (write_only == TRUE) {
261 list_add_tail(&pCifsFile->flist,
262 &pCifsInode->openFileList);
263 } else {
264 list_add(&pCifsFile->flist,
265 &pCifsInode->openFileList);
1da177e4 266 }
d14537f1
SF
267 if((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
268 pCifsInode->clientCanCacheAll = TRUE;
269 pCifsInode->clientCanCacheRead = TRUE;
270 cFYI(1,("Exclusive Oplock for inode %p",
271 newinode));
272 } else if((oplock & 0xF) == OPLOCK_READ)
273 pCifsInode->clientCanCacheRead = TRUE;
1da177e4 274 }
d14537f1 275 write_unlock(&GlobalSMBSeslock);
1da177e4
LT
276 }
277 }
d14537f1
SF
278cifs_create_out:
279 kfree(buf);
280 kfree(full_path);
1da177e4 281 FreeXid(xid);
1da177e4
LT
282 return rc;
283}
284
285int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, dev_t device_number)
286{
287 int rc = -EPERM;
288 int xid;
289 struct cifs_sb_info *cifs_sb;
290 struct cifsTconInfo *pTcon;
291 char *full_path = NULL;
292 struct inode * newinode = NULL;
293
294 if (!old_valid_dev(device_number))
295 return -EINVAL;
296
297 xid = GetXid();
298
299 cifs_sb = CIFS_SB(inode->i_sb);
300 pTcon = cifs_sb->tcon;
301
302 down(&direntry->d_sb->s_vfs_rename_sem);
ac67055e 303 full_path = build_path_from_dentry(direntry, cifs_sb);
1da177e4
LT
304 up(&direntry->d_sb->s_vfs_rename_sem);
305 if(full_path == NULL)
306 rc = -ENOMEM;
4a6d87f1 307 else if (pTcon->ses->capabilities & CAP_UNIX) {
1da177e4
LT
308 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
309 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path,
310 mode,(__u64)current->euid,(__u64)current->egid,
737b758c
SF
311 device_number, cifs_sb->local_nls,
312 cifs_sb->mnt_cifs_flags &
313 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
314 } else {
315 rc = CIFSSMBUnixSetPerms(xid, pTcon,
316 full_path, mode, (__u64)-1, (__u64)-1,
737b758c
SF
317 device_number, cifs_sb->local_nls,
318 cifs_sb->mnt_cifs_flags &
319 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
320 }
321
322 if(!rc) {
323 rc = cifs_get_inode_info_unix(&newinode, full_path,
324 inode->i_sb,xid);
325 direntry->d_op = &cifs_dentry_ops;
326 if(rc == 0)
327 d_instantiate(direntry, newinode);
328 }
d7245c2c 329 } else {
eda3c029
SF
330 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
331 int oplock = 0;
332 u16 fileHandle;
333 FILE_ALL_INFO * buf;
d7245c2c
SF
334
335 cFYI(1,("sfu compat create special file"));
d7245c2c 336
eda3c029
SF
337 buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);
338 if(buf == NULL) {
339 kfree(full_path);
340 FreeXid(xid);
341 return -ENOMEM;
342 }
343
344 rc = CIFSSMBOpen(xid, pTcon, full_path,
345 FILE_CREATE, /* fail if exists */
346 GENERIC_WRITE /* BB would
347 WRITE_OWNER | WRITE_DAC be better? */,
348 /* Create a file and set the
349 file attribute to SYSTEM */
350 CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
351 &fileHandle, &oplock, buf,
352 cifs_sb->local_nls,
353 cifs_sb->mnt_cifs_flags &
354 CIFS_MOUNT_MAP_SPECIAL_CHR);
355
356 if(!rc) {
357 /* BB Do not bother to decode buf since no
358 local inode yet to put timestamps in */
359 CIFSSMBClose(xid, pTcon, fileHandle);
360 d_drop(direntry);
361 }
362 kfree(buf);
d7245c2c
SF
363 /* add code here to set EAs */
364 }
1da177e4
LT
365 }
366
d14537f1 367 kfree(full_path);
1da177e4 368 FreeXid(xid);
1da177e4
LT
369 return rc;
370}
371
372
373struct dentry *
374cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd)
375{
376 int xid;
377 int rc = 0; /* to get around spurious gcc warning, set to zero here */
378 struct cifs_sb_info *cifs_sb;
379 struct cifsTconInfo *pTcon;
380 struct inode *newInode = NULL;
381 char *full_path = NULL;
382
383 xid = GetXid();
384
385 cFYI(1,
386 (" parent inode = 0x%p name is: %s and dentry = 0x%p",
387 parent_dir_inode, direntry->d_name.name, direntry));
388
389 /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */
390
391 /* check whether path exists */
392
393 cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
394 pTcon = cifs_sb->tcon;
395
396 /* can not grab the rename sem here since it would
397 deadlock in the cases (beginning of sys_rename itself)
398 in which we already have the sb rename sem */
ac67055e 399 full_path = build_path_from_dentry(direntry, cifs_sb);
1da177e4
LT
400 if(full_path == NULL) {
401 FreeXid(xid);
402 return ERR_PTR(-ENOMEM);
403 }
404
405 if (direntry->d_inode != NULL) {
406 cFYI(1, (" non-NULL inode in lookup"));
407 } else {
408 cFYI(1, (" NULL inode in lookup"));
409 }
410 cFYI(1,
411 (" Full path: %s inode = 0x%p", full_path, direntry->d_inode));
412
413 if (pTcon->ses->capabilities & CAP_UNIX)
414 rc = cifs_get_inode_info_unix(&newInode, full_path,
415 parent_dir_inode->i_sb,xid);
416 else
417 rc = cifs_get_inode_info(&newInode, full_path, NULL,
418 parent_dir_inode->i_sb,xid);
419
420 if ((rc == 0) && (newInode != NULL)) {
421 direntry->d_op = &cifs_dentry_ops;
422 d_add(direntry, newInode);
423
424 /* since paths are not looked up by component - the parent directories are presumed to be good here */
425 renew_parental_timestamps(direntry);
426
427 } else if (rc == -ENOENT) {
428 rc = 0;
429 d_add(direntry, NULL);
430 } else {
b2aeb9d5
SF
431 cERROR(1,("Error 0x%x on cifs_get_inode_info in lookup of %s",
432 rc,full_path));
1da177e4
LT
433 /* BB special case check for Access Denied - watch security
434 exposure of returning dir info implicitly via different rc
435 if file exists or not but no access BB */
436 }
437
d14537f1 438 kfree(full_path);
1da177e4
LT
439 FreeXid(xid);
440 return ERR_PTR(rc);
441}
442
1da177e4
LT
443static int
444cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
445{
446 int isValid = 1;
447
448/* lock_kernel(); *//* surely we do not want to lock the kernel for a whole network round trip which could take seconds */
449
450 if (direntry->d_inode) {
451 if (cifs_revalidate(direntry)) {
452 /* unlock_kernel(); */
453 return 0;
454 }
455 } else {
456 cFYI(1,
457 ("In cifs_d_revalidate with no inode but name = %s and dentry 0x%p",
458 direntry->d_name.name, direntry));
459 }
460
461/* unlock_kernel(); */
462
463 return isValid;
464}
465
466/* static int cifs_d_delete(struct dentry *direntry)
467{
468 int rc = 0;
469
470 cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
471
472 return rc;
473} */
474
475struct dentry_operations cifs_dentry_ops = {
476 .d_revalidate = cifs_d_revalidate,
477/* d_delete: cifs_d_delete, *//* not needed except for debugging */
478 /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */
479};