4 * vfs operations that deal with dentries
6 * Copyright (C) International Business Machines Corp., 2002,2005
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
)
55 return NULL
; /* not much we can do if dentry is freed and
56 we need to reopen the file after it was closed implicitly
57 when the server crashed */
59 dirsep
= CIFS_DIR_SEP(CIFS_SB(direntry
->d_sb
));
60 pplen
= CIFS_SB(direntry
->d_sb
)->prepathlen
;
63 for (temp
= direntry
; !IS_ROOT(temp
);) {
64 namelen
+= (1 + temp
->d_name
.len
);
65 temp
= temp
->d_parent
;
67 cERROR(1,("corrupt dentry"));
72 full_path
= kmalloc(namelen
+1, GFP_KERNEL
);
75 full_path
[namelen
] = 0; /* trailing null */
76 for (temp
= direntry
; !IS_ROOT(temp
);) {
77 namelen
-= 1 + temp
->d_name
.len
;
81 full_path
[namelen
] = dirsep
;
82 strncpy(full_path
+ namelen
+ 1, temp
->d_name
.name
,
84 cFYI(0, ("name: %s", full_path
+ namelen
));
86 temp
= temp
->d_parent
;
88 cERROR(1,("corrupt dentry"));
93 if (namelen
!= pplen
) {
95 ("did not end path lookup where expected namelen is %d",
97 /* presumably this is only possible if racing with a rename
98 of one of the parent directories (we can not lock the dentries
99 above us to prevent this, but retrying should be harmless) */
101 goto cifs_bp_rename_retry
;
103 /* DIR_SEP already set for byte 0 / vs \ but not for
104 subsequent slashes in prepath which currently must
105 be entered the right way - not sure if there is an alternative
106 since the '\' is a valid posix character so we can not switch
107 those safely to '/' if any are found in the middle of the prepath */
108 /* BB test paths to Windows with '/' in the midst of prepath */
109 strncpy(full_path
,CIFS_SB(direntry
->d_sb
)->prepath
,pplen
);
113 /* char * build_wildcard_path_from_dentry(struct dentry *direntry)
115 if(full_path == NULL)
118 full_path[namelen] = '\\';
119 full_path[namelen+1] = '*';
120 full_path[namelen+2] = 0;
121 BB remove above eight lines BB */
123 /* Inode operations in similar order to how they appear in Linux file fs.h */
126 cifs_create(struct inode
*inode
, struct dentry
*direntry
, int mode
,
127 struct nameidata
*nd
)
132 int desiredAccess
= GENERIC_READ
| GENERIC_WRITE
;
134 struct cifs_sb_info
*cifs_sb
;
135 struct cifsTconInfo
*pTcon
;
136 char *full_path
= NULL
;
137 FILE_ALL_INFO
* buf
= NULL
;
138 struct inode
*newinode
= NULL
;
139 struct cifsFileInfo
* pCifsFile
= NULL
;
140 struct cifsInodeInfo
* pCifsInode
;
141 int disposition
= FILE_OVERWRITE_IF
;
142 int write_only
= FALSE
;
146 cifs_sb
= CIFS_SB(inode
->i_sb
);
147 pTcon
= cifs_sb
->tcon
;
149 full_path
= build_path_from_dentry(direntry
);
150 if(full_path
== NULL
) {
155 if(nd
&& (nd
->flags
& LOOKUP_OPEN
)) {
156 int oflags
= nd
->intent
.open
.flags
;
159 if (oflags
& FMODE_READ
)
160 desiredAccess
|= GENERIC_READ
;
161 if (oflags
& FMODE_WRITE
) {
162 desiredAccess
|= GENERIC_WRITE
;
163 if (!(oflags
& FMODE_READ
))
167 if((oflags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
))
168 disposition
= FILE_CREATE
;
169 else if((oflags
& (O_CREAT
| O_TRUNC
)) == (O_CREAT
| O_TRUNC
))
170 disposition
= FILE_OVERWRITE_IF
;
171 else if((oflags
& O_CREAT
) == O_CREAT
)
172 disposition
= FILE_OPEN_IF
;
174 cFYI(1,("Create flag not set in create function"));
178 /* BB add processing to set equivalent of mode - e.g. via CreateX with ACLs */
182 buf
= kmalloc(sizeof(FILE_ALL_INFO
),GFP_KERNEL
);
188 if (cifs_sb
->tcon
->ses
->capabilities
& CAP_NT_SMBS
)
189 rc
= CIFSSMBOpen(xid
, pTcon
, full_path
, disposition
,
190 desiredAccess
, CREATE_NOT_DIR
,
191 &fileHandle
, &oplock
, buf
, cifs_sb
->local_nls
,
192 cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
);
194 rc
= -EIO
; /* no NT SMB support fall into legacy open below */
197 /* old server, retry the open legacy style */
198 rc
= SMBLegacyOpen(xid
, pTcon
, full_path
, disposition
,
199 desiredAccess
, CREATE_NOT_DIR
,
200 &fileHandle
, &oplock
, buf
, cifs_sb
->local_nls
,
201 cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
);
204 cFYI(1, ("cifs_create returned 0x%x", rc
));
206 /* If Open reported that we actually created a file
207 then we now have to set the mode if possible */
208 if ((cifs_sb
->tcon
->ses
->capabilities
& CAP_UNIX
) &&
209 (oplock
& CIFS_CREATE_ACTION
))
210 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SET_UID
) {
211 CIFSSMBUnixSetPerms(xid
, pTcon
, full_path
, mode
,
212 (__u64
)current
->fsuid
,
213 (__u64
)current
->fsgid
,
216 cifs_sb
->mnt_cifs_flags
&
217 CIFS_MOUNT_MAP_SPECIAL_CHR
);
219 CIFSSMBUnixSetPerms(xid
, pTcon
, full_path
, mode
,
224 cifs_sb
->mnt_cifs_flags
&
225 CIFS_MOUNT_MAP_SPECIAL_CHR
);
228 /* BB implement mode setting via Windows security descriptors */
229 /* eg CIFSSMBWinSetPerms(xid,pTcon,full_path,mode,-1,-1,local_nls);*/
230 /* could set r/o dos attribute if mode & 0222 == 0 */
233 /* BB server might mask mode so we have to query for Unix case*/
234 if (pTcon
->ses
->capabilities
& CAP_UNIX
)
235 rc
= cifs_get_inode_info_unix(&newinode
, full_path
,
238 rc
= cifs_get_inode_info(&newinode
, full_path
,
239 buf
, inode
->i_sb
,xid
);
241 newinode
->i_mode
= mode
;
242 if((oplock
& CIFS_CREATE_ACTION
) &&
243 (cifs_sb
->mnt_cifs_flags
&
244 CIFS_MOUNT_SET_UID
)) {
245 newinode
->i_uid
= current
->fsuid
;
246 newinode
->i_gid
= current
->fsgid
;
253 ("Create worked but get_inode_info failed rc = %d",
257 direntry
->d_op
= &cifs_ci_dentry_ops
;
259 direntry
->d_op
= &cifs_dentry_ops
;
260 d_instantiate(direntry
, newinode
);
262 if((nd
->flags
& LOOKUP_OPEN
) == FALSE
) {
263 /* mknod case - do not leave file open */
264 CIFSSMBClose(xid
, pTcon
, fileHandle
);
265 } else if(newinode
) {
267 kzalloc(sizeof (struct cifsFileInfo
), GFP_KERNEL
);
269 if(pCifsFile
== NULL
)
270 goto cifs_create_out
;
271 pCifsFile
->netfid
= fileHandle
;
272 pCifsFile
->pid
= current
->tgid
;
273 pCifsFile
->pInode
= newinode
;
274 pCifsFile
->invalidHandle
= FALSE
;
275 pCifsFile
->closePend
= FALSE
;
276 init_MUTEX(&pCifsFile
->fh_sem
);
277 init_MUTEX(&pCifsFile
->lock_sem
);
278 INIT_LIST_HEAD(&pCifsFile
->llist
);
279 atomic_set(&pCifsFile
->wrtPending
,0);
281 /* set the following in open now
282 pCifsFile->pfile = file; */
283 write_lock(&GlobalSMBSeslock
);
284 list_add(&pCifsFile
->tlist
,&pTcon
->openFileList
);
285 pCifsInode
= CIFS_I(newinode
);
287 /* if readable file instance put first in list*/
288 if (write_only
== TRUE
) {
289 list_add_tail(&pCifsFile
->flist
,
290 &pCifsInode
->openFileList
);
292 list_add(&pCifsFile
->flist
,
293 &pCifsInode
->openFileList
);
295 if((oplock
& 0xF) == OPLOCK_EXCLUSIVE
) {
296 pCifsInode
->clientCanCacheAll
= TRUE
;
297 pCifsInode
->clientCanCacheRead
= TRUE
;
298 cFYI(1,("Exclusive Oplock for inode %p",
300 } else if((oplock
& 0xF) == OPLOCK_READ
)
301 pCifsInode
->clientCanCacheRead
= TRUE
;
303 write_unlock(&GlobalSMBSeslock
);
313 int cifs_mknod(struct inode
*inode
, struct dentry
*direntry
, int mode
,
318 struct cifs_sb_info
*cifs_sb
;
319 struct cifsTconInfo
*pTcon
;
320 char *full_path
= NULL
;
321 struct inode
* newinode
= NULL
;
323 if (!old_valid_dev(device_number
))
328 cifs_sb
= CIFS_SB(inode
->i_sb
);
329 pTcon
= cifs_sb
->tcon
;
331 full_path
= build_path_from_dentry(direntry
);
332 if(full_path
== NULL
)
334 else if (pTcon
->ses
->capabilities
& CAP_UNIX
) {
335 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SET_UID
) {
336 rc
= CIFSSMBUnixSetPerms(xid
, pTcon
, full_path
,
337 mode
,(__u64
)current
->fsuid
,(__u64
)current
->fsgid
,
338 device_number
, cifs_sb
->local_nls
,
339 cifs_sb
->mnt_cifs_flags
&
340 CIFS_MOUNT_MAP_SPECIAL_CHR
);
342 rc
= CIFSSMBUnixSetPerms(xid
, pTcon
,
343 full_path
, mode
, (__u64
)-1, (__u64
)-1,
344 device_number
, cifs_sb
->local_nls
,
345 cifs_sb
->mnt_cifs_flags
&
346 CIFS_MOUNT_MAP_SPECIAL_CHR
);
350 rc
= cifs_get_inode_info_unix(&newinode
, full_path
,
353 direntry
->d_op
= &cifs_ci_dentry_ops
;
355 direntry
->d_op
= &cifs_dentry_ops
;
357 d_instantiate(direntry
, newinode
);
360 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_UNX_EMUL
) {
365 cFYI(1,("sfu compat create special file"));
367 buf
= kmalloc(sizeof(FILE_ALL_INFO
),GFP_KERNEL
);
374 rc
= CIFSSMBOpen(xid
, pTcon
, full_path
,
375 FILE_CREATE
, /* fail if exists */
376 GENERIC_WRITE
/* BB would
377 WRITE_OWNER | WRITE_DAC be better? */,
378 /* Create a file and set the
379 file attribute to SYSTEM */
380 CREATE_NOT_DIR
| CREATE_OPTION_SPECIAL
,
381 &fileHandle
, &oplock
, buf
,
383 cifs_sb
->mnt_cifs_flags
&
384 CIFS_MOUNT_MAP_SPECIAL_CHR
);
386 /* BB FIXME - add handling for backlevel servers
387 which need legacy open and check for all
388 calls to SMBOpen for fallback to
391 /* BB Do not bother to decode buf since no
392 local inode yet to put timestamps in,
393 but we can reuse it safely */
395 struct win_dev
*pdev
;
396 pdev
= (struct win_dev
*)buf
;
398 memcpy(pdev
->type
, "IntxCHR", 8);
400 cpu_to_le64(MAJOR(device_number
));
402 cpu_to_le64(MINOR(device_number
));
403 rc
= CIFSSMBWrite(xid
, pTcon
,
405 sizeof(struct win_dev
),
406 0, &bytes_written
, (char *)pdev
,
408 } else if(S_ISBLK(mode
)) {
409 memcpy(pdev
->type
, "IntxBLK", 8);
411 cpu_to_le64(MAJOR(device_number
));
413 cpu_to_le64(MINOR(device_number
));
414 rc
= CIFSSMBWrite(xid
, pTcon
,
416 sizeof(struct win_dev
),
417 0, &bytes_written
, (char *)pdev
,
419 } /* else if(S_ISFIFO */
420 CIFSSMBClose(xid
, pTcon
, fileHandle
);
424 /* add code here to set EAs */
435 cifs_lookup(struct inode
*parent_dir_inode
, struct dentry
*direntry
, struct nameidata
*nd
)
438 int rc
= 0; /* to get around spurious gcc warning, set to zero here */
439 struct cifs_sb_info
*cifs_sb
;
440 struct cifsTconInfo
*pTcon
;
441 struct inode
*newInode
= NULL
;
442 char *full_path
= NULL
;
447 (" parent inode = 0x%p name is: %s and dentry = 0x%p",
448 parent_dir_inode
, direntry
->d_name
.name
, direntry
));
450 /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */
452 /* check whether path exists */
454 cifs_sb
= CIFS_SB(parent_dir_inode
->i_sb
);
455 pTcon
= cifs_sb
->tcon
;
458 * Don't allow the separator character in a path component.
459 * The VFS will not allow "/", but "\" is allowed by posix.
461 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_POSIX_PATHS
)) {
463 for (i
= 0; i
< direntry
->d_name
.len
; i
++)
464 if (direntry
->d_name
.name
[i
] == '\\') {
465 cFYI(1, ("Invalid file name"));
467 return ERR_PTR(-EINVAL
);
471 /* can not grab the rename sem here since it would
472 deadlock in the cases (beginning of sys_rename itself)
473 in which we already have the sb rename sem */
474 full_path
= build_path_from_dentry(direntry
);
475 if(full_path
== NULL
) {
477 return ERR_PTR(-ENOMEM
);
480 if (direntry
->d_inode
!= NULL
) {
481 cFYI(1, (" non-NULL inode in lookup"));
483 cFYI(1, (" NULL inode in lookup"));
486 (" Full path: %s inode = 0x%p", full_path
, direntry
->d_inode
));
488 if (pTcon
->ses
->capabilities
& CAP_UNIX
)
489 rc
= cifs_get_inode_info_unix(&newInode
, full_path
,
490 parent_dir_inode
->i_sb
,xid
);
492 rc
= cifs_get_inode_info(&newInode
, full_path
, NULL
,
493 parent_dir_inode
->i_sb
,xid
);
495 if ((rc
== 0) && (newInode
!= NULL
)) {
497 direntry
->d_op
= &cifs_ci_dentry_ops
;
499 direntry
->d_op
= &cifs_dentry_ops
;
500 d_add(direntry
, newInode
);
502 /* since paths are not looked up by component - the parent
503 directories are presumed to be good here */
504 renew_parental_timestamps(direntry
);
506 } else if (rc
== -ENOENT
) {
508 direntry
->d_time
= jiffies
;
510 direntry
->d_op
= &cifs_ci_dentry_ops
;
512 direntry
->d_op
= &cifs_dentry_ops
;
513 d_add(direntry
, NULL
);
514 /* if it was once a directory (but how can we tell?) we could do
515 shrink_dcache_parent(direntry); */
517 cERROR(1,("Error 0x%x on cifs_get_inode_info in lookup of %s",
519 /* BB special case check for Access Denied - watch security
520 exposure of returning dir info implicitly via different rc
521 if file exists or not but no access BB */
530 cifs_d_revalidate(struct dentry
*direntry
, struct nameidata
*nd
)
534 if (direntry
->d_inode
) {
535 if (cifs_revalidate(direntry
)) {
539 cFYI(1, ("neg dentry 0x%p name = %s",
540 direntry
, direntry
->d_name
.name
));
541 if(time_after(jiffies
, direntry
->d_time
+ HZ
) ||
542 !lookupCacheEnabled
) {
551 /* static int cifs_d_delete(struct dentry *direntry)
555 cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
560 struct dentry_operations cifs_dentry_ops
= {
561 .d_revalidate
= cifs_d_revalidate
,
562 /* d_delete: cifs_d_delete, *//* not needed except for debugging */
563 /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */
566 static int cifs_ci_hash(struct dentry
*dentry
, struct qstr
*q
)
568 struct nls_table
*codepage
= CIFS_SB(dentry
->d_inode
->i_sb
)->local_nls
;
572 hash
= init_name_hash();
573 for (i
= 0; i
< q
->len
; i
++)
574 hash
= partial_name_hash(nls_tolower(codepage
, q
->name
[i
]),
576 q
->hash
= end_name_hash(hash
);
581 static int cifs_ci_compare(struct dentry
*dentry
, struct qstr
*a
,
584 struct nls_table
*codepage
= CIFS_SB(dentry
->d_inode
->i_sb
)->local_nls
;
586 if ((a
->len
== b
->len
) &&
587 (nls_strnicmp(codepage
, a
->name
, b
->name
, a
->len
) == 0)) {
589 * To preserve case, don't let an existing negative dentry's
590 * case take precedence. If a is not a negative dentry, this
591 * should have no side effects
593 memcpy((unsigned char *)a
->name
, b
->name
, a
->len
);
599 struct dentry_operations cifs_ci_dentry_ops
= {
600 .d_revalidate
= cifs_d_revalidate
,
601 .d_hash
= cifs_ci_hash
,
602 .d_compare
= cifs_ci_compare
,