4 * vfs operations that deal with dentries
6 * Copyright (C) International Business Machines Corp., 2002,2003
7 * Author(s): Steve French (sfrench@us.ibm.com)
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.
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.
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
24 #include <linux/stat.h>
25 #include <linux/slab.h>
26 #include <linux/namei.h>
30 #include "cifsproto.h"
31 #include "cifs_debug.h"
32 #include "cifs_fs_sb.h"
35 renew_parental_timestamps(struct dentry
*direntry
)
37 /* BB check if there is a way to get the kernel to do this or if we really need this */
39 direntry
->d_time
= jiffies
;
40 direntry
= direntry
->d_parent
;
41 } while (!IS_ROOT(direntry
));
44 /* Note: caller must free return buffer */
46 build_path_from_dentry(struct dentry
*direntry
)
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 */
58 for (temp
= direntry
; !IS_ROOT(temp
);) {
59 namelen
+= (1 + temp
->d_name
.len
);
60 temp
= temp
->d_parent
;
62 cERROR(1,("corrupt dentry"));
67 full_path
= kmalloc(namelen
+1, GFP_KERNEL
);
70 full_path
[namelen
] = 0; /* trailing null */
72 for (temp
= direntry
; !IS_ROOT(temp
);) {
73 namelen
-= 1 + temp
->d_name
.len
;
77 full_path
[namelen
] = '\\';
78 strncpy(full_path
+ namelen
+ 1, temp
->d_name
.name
,
80 cFYI(0, (" name: %s ", full_path
+ namelen
));
82 temp
= temp
->d_parent
;
84 cERROR(1,("corrupt dentry"));
91 ("We did not end path lookup where we expected namelen is %d",
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) */
98 goto cifs_bp_rename_retry
;
104 /* Note: caller must free return buffer */
106 build_wildcard_path_from_dentry(struct dentry
*direntry
)
113 return NULL
; /* not much we can do if dentry is freed and
114 we need to reopen the file after it was closed implicitly
115 when the server crashed */
117 cifs_bwp_rename_retry
:
118 for (temp
= direntry
; !IS_ROOT(temp
);) {
119 namelen
+= (1 + temp
->d_name
.len
);
120 temp
= temp
->d_parent
;
122 cERROR(1,("corrupt dentry"));
127 full_path
= kmalloc(namelen
+3, GFP_KERNEL
);
128 if(full_path
== NULL
)
131 full_path
[namelen
] = '\\';
132 full_path
[namelen
+1] = '*';
133 full_path
[namelen
+2] = 0; /* trailing null */
135 for (temp
= direntry
; !IS_ROOT(temp
);) {
136 namelen
-= 1 + temp
->d_name
.len
;
140 full_path
[namelen
] = '\\';
141 strncpy(full_path
+ namelen
+ 1, temp
->d_name
.name
,
143 cFYI(0, (" name: %s ", full_path
+ namelen
));
145 temp
= temp
->d_parent
;
147 cERROR(1,("corrupt dentry"));
154 ("We did not end path lookup where we expected namelen is %d",
156 /* presumably this is only possible if we were racing with a rename
157 of one of the parent directories (we can not lock the dentries
158 above us to prevent this, but retrying should be harmless) */
161 goto cifs_bwp_rename_retry
;
167 /* Inode operations in similar order to how they appear in the Linux file fs.h */
170 cifs_create(struct inode
*inode
, struct dentry
*direntry
, int mode
,
171 struct nameidata
*nd
)
176 int desiredAccess
= GENERIC_READ
| GENERIC_WRITE
;
178 struct cifs_sb_info
*cifs_sb
;
179 struct cifsTconInfo
*pTcon
;
180 char *full_path
= NULL
;
181 FILE_ALL_INFO
* buf
= NULL
;
182 struct inode
*newinode
= NULL
;
183 struct cifsFileInfo
* pCifsFile
= NULL
;
184 struct cifsInodeInfo
* pCifsInode
;
185 int disposition
= FILE_OVERWRITE_IF
;
186 int write_only
= FALSE
;
190 cifs_sb
= CIFS_SB(inode
->i_sb
);
191 pTcon
= cifs_sb
->tcon
;
193 down(&direntry
->d_sb
->s_vfs_rename_sem
);
194 full_path
= build_path_from_dentry(direntry
);
195 up(&direntry
->d_sb
->s_vfs_rename_sem
);
196 if(full_path
== NULL
) {
202 if ((nd
->intent
.open
.flags
& O_ACCMODE
) == O_RDONLY
)
203 desiredAccess
= GENERIC_READ
;
204 else if ((nd
->intent
.open
.flags
& O_ACCMODE
) == O_WRONLY
) {
205 desiredAccess
= GENERIC_WRITE
;
207 } else if ((nd
->intent
.open
.flags
& O_ACCMODE
) == O_RDWR
) {
208 /* GENERIC_ALL is too much permission to request */
209 /* can cause unnecessary access denied on create */
210 /* desiredAccess = GENERIC_ALL; */
211 desiredAccess
= GENERIC_READ
| GENERIC_WRITE
;
214 if((nd
->intent
.open
.flags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
))
215 disposition
= FILE_CREATE
;
216 else if((nd
->intent
.open
.flags
& (O_CREAT
| O_TRUNC
)) == (O_CREAT
| O_TRUNC
))
217 disposition
= FILE_OVERWRITE_IF
;
218 else if((nd
->intent
.open
.flags
& O_CREAT
) == O_CREAT
)
219 disposition
= FILE_OPEN_IF
;
221 cFYI(1,("Create flag not set in create function"));
225 /* BB add processing to set equivalent of mode - e.g. via CreateX with ACLs */
229 buf
= kmalloc(sizeof(FILE_ALL_INFO
),GFP_KERNEL
);
236 rc
= CIFSSMBOpen(xid
, pTcon
, full_path
, disposition
,
237 desiredAccess
, CREATE_NOT_DIR
,
238 &fileHandle
, &oplock
, buf
, cifs_sb
->local_nls
);
240 cFYI(1, ("cifs_create returned 0x%x ", rc
));
242 /* If Open reported that we actually created a file
243 then we now have to set the mode if possible */
244 if ((cifs_sb
->tcon
->ses
->capabilities
& CAP_UNIX
) &&
245 (oplock
& CIFS_CREATE_ACTION
))
246 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SET_UID
) {
247 CIFSSMBUnixSetPerms(xid
, pTcon
, full_path
, mode
,
248 (__u64
)current
->euid
,
249 (__u64
)current
->egid
,
253 CIFSSMBUnixSetPerms(xid
, pTcon
, full_path
, mode
,
260 /* BB implement via Windows security descriptors */
261 /* eg CIFSSMBWinSetPerms(xid,pTcon,full_path,mode,-1,-1,local_nls);*/
262 /* could set r/o dos attribute if mode & 0222 == 0 */
265 /* BB server might mask mode so we have to query for Unix case*/
266 if (pTcon
->ses
->capabilities
& CAP_UNIX
)
267 rc
= cifs_get_inode_info_unix(&newinode
, full_path
,
270 rc
= cifs_get_inode_info(&newinode
, full_path
,
271 buf
, inode
->i_sb
,xid
);
273 newinode
->i_mode
= mode
;
277 cFYI(1,("Create worked but get_inode_info failed with rc = %d",
280 direntry
->d_op
= &cifs_dentry_ops
;
281 d_instantiate(direntry
, newinode
);
283 if((nd
->flags
& LOOKUP_OPEN
) == FALSE
) {
284 /* mknod case - do not leave file open */
285 CIFSSMBClose(xid
, pTcon
, fileHandle
);
286 } else if(newinode
) {
287 pCifsFile
= (struct cifsFileInfo
*)
288 kmalloc(sizeof (struct cifsFileInfo
), GFP_KERNEL
);
291 memset((char *)pCifsFile
, 0,
292 sizeof (struct cifsFileInfo
));
293 pCifsFile
->netfid
= fileHandle
;
294 pCifsFile
->pid
= current
->tgid
;
295 pCifsFile
->pInode
= newinode
;
296 pCifsFile
->invalidHandle
= FALSE
;
297 pCifsFile
->closePend
= FALSE
;
298 init_MUTEX(&pCifsFile
->fh_sem
);
299 /* put the following in at open now */
300 /* pCifsFile->pfile = file; */
301 write_lock(&GlobalSMBSeslock
);
302 list_add(&pCifsFile
->tlist
,&pTcon
->openFileList
);
303 pCifsInode
= CIFS_I(newinode
);
305 /* if readable file instance put first in list*/
306 if (write_only
== TRUE
) {
307 list_add_tail(&pCifsFile
->flist
,
308 &pCifsInode
->openFileList
);
310 list_add(&pCifsFile
->flist
,
311 &pCifsInode
->openFileList
);
313 if((oplock
& 0xF) == OPLOCK_EXCLUSIVE
) {
314 pCifsInode
->clientCanCacheAll
= TRUE
;
315 pCifsInode
->clientCanCacheRead
= TRUE
;
316 cFYI(1,("Exclusive Oplock granted on inode %p",
318 } else if((oplock
& 0xF) == OPLOCK_READ
)
319 pCifsInode
->clientCanCacheRead
= TRUE
;
321 write_unlock(&GlobalSMBSeslock
);
335 int cifs_mknod(struct inode
*inode
, struct dentry
*direntry
, int mode
, dev_t device_number
)
339 struct cifs_sb_info
*cifs_sb
;
340 struct cifsTconInfo
*pTcon
;
341 char *full_path
= NULL
;
342 struct inode
* newinode
= NULL
;
344 if (!old_valid_dev(device_number
))
349 cifs_sb
= CIFS_SB(inode
->i_sb
);
350 pTcon
= cifs_sb
->tcon
;
352 down(&direntry
->d_sb
->s_vfs_rename_sem
);
353 full_path
= build_path_from_dentry(direntry
);
354 up(&direntry
->d_sb
->s_vfs_rename_sem
);
355 if(full_path
== NULL
)
358 if (full_path
&& (pTcon
->ses
->capabilities
& CAP_UNIX
)) {
359 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SET_UID
) {
360 rc
= CIFSSMBUnixSetPerms(xid
, pTcon
, full_path
,
361 mode
,(__u64
)current
->euid
,(__u64
)current
->egid
,
362 device_number
, cifs_sb
->local_nls
);
364 rc
= CIFSSMBUnixSetPerms(xid
, pTcon
,
365 full_path
, mode
, (__u64
)-1, (__u64
)-1,
366 device_number
, cifs_sb
->local_nls
);
370 rc
= cifs_get_inode_info_unix(&newinode
, full_path
,
372 direntry
->d_op
= &cifs_dentry_ops
;
374 d_instantiate(direntry
, newinode
);
387 cifs_lookup(struct inode
*parent_dir_inode
, struct dentry
*direntry
, struct nameidata
*nd
)
390 int rc
= 0; /* to get around spurious gcc warning, set to zero here */
391 struct cifs_sb_info
*cifs_sb
;
392 struct cifsTconInfo
*pTcon
;
393 struct inode
*newInode
= NULL
;
394 char *full_path
= NULL
;
399 (" parent inode = 0x%p name is: %s and dentry = 0x%p",
400 parent_dir_inode
, direntry
->d_name
.name
, direntry
));
402 if(nd
) { /* BB removeme */
403 cFYI(1,("In lookup nd flags 0x%x open intent flags 0x%x",nd
->flags
,nd
->intent
.open
.flags
));
404 } /* BB removeme BB */
405 /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */
407 /* check whether path exists */
409 cifs_sb
= CIFS_SB(parent_dir_inode
->i_sb
);
410 pTcon
= cifs_sb
->tcon
;
412 /* can not grab the rename sem here since it would
413 deadlock in the cases (beginning of sys_rename itself)
414 in which we already have the sb rename sem */
415 full_path
= build_path_from_dentry(direntry
);
416 if(full_path
== NULL
) {
418 return ERR_PTR(-ENOMEM
);
421 if (direntry
->d_inode
!= NULL
) {
422 cFYI(1, (" non-NULL inode in lookup"));
424 cFYI(1, (" NULL inode in lookup"));
427 (" Full path: %s inode = 0x%p", full_path
, direntry
->d_inode
));
429 if (pTcon
->ses
->capabilities
& CAP_UNIX
)
430 rc
= cifs_get_inode_info_unix(&newInode
, full_path
,
431 parent_dir_inode
->i_sb
,xid
);
433 rc
= cifs_get_inode_info(&newInode
, full_path
, NULL
,
434 parent_dir_inode
->i_sb
,xid
);
436 if ((rc
== 0) && (newInode
!= NULL
)) {
437 direntry
->d_op
= &cifs_dentry_ops
;
438 d_add(direntry
, newInode
);
440 /* since paths are not looked up by component - the parent directories are presumed to be good here */
441 renew_parental_timestamps(direntry
);
443 } else if (rc
== -ENOENT
) {
445 d_add(direntry
, NULL
);
447 cERROR(1,("Error 0x%x or on cifs_get_inode_info in lookup",rc
));
448 /* BB special case check for Access Denied - watch security
449 exposure of returning dir info implicitly via different rc
450 if file exists or not but no access BB */
460 cifs_dir_open(struct inode
*inode
, struct file
*file
)
461 { /* NB: currently unused since searches are opened in readdir */
464 struct cifs_sb_info
*cifs_sb
;
465 struct cifsTconInfo
*pTcon
;
466 char *full_path
= NULL
;
470 cifs_sb
= CIFS_SB(inode
->i_sb
);
471 pTcon
= cifs_sb
->tcon
;
474 down(&file
->f_dentry
->d_sb
->s_vfs_rename_sem
);
475 full_path
= build_wildcard_path_from_dentry(file
->f_dentry
);
476 up(&file
->f_dentry
->d_sb
->s_vfs_rename_sem
);
482 cFYI(1, ("inode = 0x%p and full path is %s", inode
, full_path
));
491 cifs_d_revalidate(struct dentry
*direntry
, struct nameidata
*nd
)
495 /* lock_kernel(); *//* surely we do not want to lock the kernel for a whole network round trip which could take seconds */
497 if (direntry
->d_inode
) {
498 if (cifs_revalidate(direntry
)) {
499 /* unlock_kernel(); */
504 ("In cifs_d_revalidate with no inode but name = %s and dentry 0x%p",
505 direntry
->d_name
.name
, direntry
));
508 /* unlock_kernel(); */
513 /* static int cifs_d_delete(struct dentry *direntry)
517 cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
522 struct dentry_operations cifs_dentry_ops
= {
523 .d_revalidate
= cifs_d_revalidate
,
524 /* d_delete: cifs_d_delete, *//* not needed except for debugging */
525 /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */