4 * vfs operations that deal with dentries
6 * Copyright (C) International Business Machines Corp., 2002,2008
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
40 direntry
->d_time
= jiffies
;
41 direntry
= direntry
->d_parent
;
42 } while (!IS_ROOT(direntry
));
45 /* Note: caller must free return buffer */
47 build_path_from_dentry(struct dentry
*direntry
)
55 struct cifs_sb_info
*cifs_sb
;
58 return NULL
; /* not much we can do if dentry is freed and
59 we need to reopen the file after it was closed implicitly
60 when the server crashed */
62 cifs_sb
= CIFS_SB(direntry
->d_sb
);
63 dirsep
= CIFS_DIR_SEP(cifs_sb
);
64 pplen
= cifs_sb
->prepathlen
;
65 if (cifs_sb
->tcon
&& (cifs_sb
->tcon
->Flags
& SMB_SHARE_IS_IN_DFS
))
66 dfsplen
= strnlen(cifs_sb
->tcon
->treeName
, MAX_TREE_SIZE
+ 1);
70 namelen
= pplen
+ dfsplen
;
71 for (temp
= direntry
; !IS_ROOT(temp
);) {
72 namelen
+= (1 + temp
->d_name
.len
);
73 temp
= temp
->d_parent
;
75 cERROR(1, ("corrupt dentry"));
80 full_path
= kmalloc(namelen
+1, GFP_KERNEL
);
81 if (full_path
== NULL
)
83 full_path
[namelen
] = 0; /* trailing null */
84 for (temp
= direntry
; !IS_ROOT(temp
);) {
85 namelen
-= 1 + temp
->d_name
.len
;
89 full_path
[namelen
] = dirsep
;
90 strncpy(full_path
+ namelen
+ 1, temp
->d_name
.name
,
92 cFYI(0, ("name: %s", full_path
+ namelen
));
94 temp
= temp
->d_parent
;
96 cERROR(1, ("corrupt dentry"));
101 if (namelen
!= pplen
+ dfsplen
) {
103 ("did not end path lookup where expected namelen is %d",
105 /* presumably this is only possible if racing with a rename
106 of one of the parent directories (we can not lock the dentries
107 above us to prevent this, but retrying should be harmless) */
109 goto cifs_bp_rename_retry
;
111 /* DIR_SEP already set for byte 0 / vs \ but not for
112 subsequent slashes in prepath which currently must
113 be entered the right way - not sure if there is an alternative
114 since the '\' is a valid posix character so we can not switch
115 those safely to '/' if any are found in the middle of the prepath */
116 /* BB test paths to Windows with '/' in the midst of prepath */
119 strncpy(full_path
, cifs_sb
->tcon
->treeName
, dfsplen
);
120 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_POSIX_PATHS
) {
122 for (i
= 0; i
< dfsplen
; i
++) {
123 if (full_path
[i
] == '\\')
128 strncpy(full_path
+ dfsplen
, CIFS_SB(direntry
->d_sb
)->prepath
, pplen
);
132 static void setup_cifs_dentry(struct cifsTconInfo
*tcon
,
133 struct dentry
*direntry
,
134 struct inode
*newinode
)
137 direntry
->d_op
= &cifs_ci_dentry_ops
;
139 direntry
->d_op
= &cifs_dentry_ops
;
140 d_instantiate(direntry
, newinode
);
143 /* Inode operations in similar order to how they appear in Linux file fs.h */
146 cifs_create(struct inode
*inode
, struct dentry
*direntry
, int mode
,
147 struct nameidata
*nd
)
151 int create_options
= CREATE_NOT_DIR
;
153 /* BB below access is too much for the mknod to request */
154 int desiredAccess
= GENERIC_READ
| GENERIC_WRITE
;
156 struct cifs_sb_info
*cifs_sb
;
157 struct cifsTconInfo
*tcon
;
158 char *full_path
= NULL
;
159 FILE_ALL_INFO
*buf
= NULL
;
160 struct inode
*newinode
= NULL
;
161 struct cifsInodeInfo
*pCifsInode
;
162 int disposition
= FILE_OVERWRITE_IF
;
163 bool write_only
= false;
167 cifs_sb
= CIFS_SB(inode
->i_sb
);
168 tcon
= cifs_sb
->tcon
;
170 full_path
= build_path_from_dentry(direntry
);
171 if (full_path
== NULL
) {
176 mode
&= ~current
->fs
->umask
;
178 if (nd
&& (nd
->flags
& LOOKUP_OPEN
)) {
179 int oflags
= nd
->intent
.open
.flags
;
182 if (oflags
& FMODE_READ
)
183 desiredAccess
|= GENERIC_READ
;
184 if (oflags
& FMODE_WRITE
) {
185 desiredAccess
|= GENERIC_WRITE
;
186 if (!(oflags
& FMODE_READ
))
190 if ((oflags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
))
191 disposition
= FILE_CREATE
;
192 else if ((oflags
& (O_CREAT
| O_TRUNC
)) == (O_CREAT
| O_TRUNC
))
193 disposition
= FILE_OVERWRITE_IF
;
194 else if ((oflags
& O_CREAT
) == O_CREAT
)
195 disposition
= FILE_OPEN_IF
;
197 cFYI(1, ("Create flag not set in create function"));
200 /* BB add processing to set equivalent of mode - e.g. via CreateX with
205 buf
= kmalloc(sizeof(FILE_ALL_INFO
), GFP_KERNEL
);
213 * if we're not using unix extensions, see if we need to set
214 * ATTR_READONLY on the create call
216 if (!tcon
->unix_ext
&& (mode
& S_IWUGO
) == 0)
217 create_options
|= CREATE_OPTION_READONLY
;
219 if (cifs_sb
->tcon
->ses
->capabilities
& CAP_NT_SMBS
)
220 rc
= CIFSSMBOpen(xid
, tcon
, full_path
, disposition
,
221 desiredAccess
, create_options
,
222 &fileHandle
, &oplock
, buf
, cifs_sb
->local_nls
,
223 cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
);
225 rc
= -EIO
; /* no NT SMB support fall into legacy open below */
228 /* old server, retry the open legacy style */
229 rc
= SMBLegacyOpen(xid
, tcon
, full_path
, disposition
,
230 desiredAccess
, create_options
,
231 &fileHandle
, &oplock
, buf
, cifs_sb
->local_nls
,
232 cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
);
235 cFYI(1, ("cifs_create returned 0x%x", rc
));
237 /* If Open reported that we actually created a file
238 then we now have to set the mode if possible */
239 if ((tcon
->unix_ext
) && (oplock
& CIFS_CREATE_ACTION
)) {
240 struct cifs_unix_set_info_args args
= {
242 .ctime
= NO_CHANGE_64
,
243 .atime
= NO_CHANGE_64
,
244 .mtime
= NO_CHANGE_64
,
248 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SET_UID
) {
249 args
.uid
= (__u64
) current_fsuid();
250 if (inode
->i_mode
& S_ISGID
)
251 args
.gid
= (__u64
) inode
->i_gid
;
253 args
.gid
= (__u64
) current_fsgid();
255 args
.uid
= NO_CHANGE_64
;
256 args
.gid
= NO_CHANGE_64
;
258 CIFSSMBUnixSetInfo(xid
, tcon
, full_path
, &args
,
260 cifs_sb
->mnt_cifs_flags
&
261 CIFS_MOUNT_MAP_SPECIAL_CHR
);
263 /* BB implement mode setting via Windows security
265 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
267 /* Could set r/o dos attribute if mode & 0222 == 0 */
270 /* server might mask mode so we have to query for it */
272 rc
= cifs_get_inode_info_unix(&newinode
, full_path
,
275 rc
= cifs_get_inode_info(&newinode
, full_path
,
276 buf
, inode
->i_sb
, xid
,
279 if (cifs_sb
->mnt_cifs_flags
&
281 newinode
->i_mode
= mode
;
282 if ((oplock
& CIFS_CREATE_ACTION
) &&
283 (cifs_sb
->mnt_cifs_flags
&
284 CIFS_MOUNT_SET_UID
)) {
285 newinode
->i_uid
= current_fsuid();
286 if (inode
->i_mode
& S_ISGID
)
297 cFYI(1, ("Create worked, get_inode_info failed rc = %d",
300 setup_cifs_dentry(tcon
, direntry
, newinode
);
302 if ((nd
== NULL
/* nfsd case - nfs srv does not set nd */) ||
303 (!(nd
->flags
& LOOKUP_OPEN
))) {
304 /* mknod case - do not leave file open */
305 CIFSSMBClose(xid
, tcon
, fileHandle
);
306 } else if (newinode
) {
307 struct cifsFileInfo
*pCifsFile
=
308 kzalloc(sizeof(struct cifsFileInfo
), GFP_KERNEL
);
310 if (pCifsFile
== NULL
)
311 goto cifs_create_out
;
312 pCifsFile
->netfid
= fileHandle
;
313 pCifsFile
->pid
= current
->tgid
;
314 pCifsFile
->pInode
= newinode
;
315 pCifsFile
->invalidHandle
= false;
316 pCifsFile
->closePend
= false;
317 init_MUTEX(&pCifsFile
->fh_sem
);
318 mutex_init(&pCifsFile
->lock_mutex
);
319 INIT_LIST_HEAD(&pCifsFile
->llist
);
320 atomic_set(&pCifsFile
->wrtPending
, 0);
322 /* set the following in open now
323 pCifsFile->pfile = file; */
324 write_lock(&GlobalSMBSeslock
);
325 list_add(&pCifsFile
->tlist
, &tcon
->openFileList
);
326 pCifsInode
= CIFS_I(newinode
);
328 /* if readable file instance put first in list*/
330 list_add_tail(&pCifsFile
->flist
,
331 &pCifsInode
->openFileList
);
333 list_add(&pCifsFile
->flist
,
334 &pCifsInode
->openFileList
);
336 if ((oplock
& 0xF) == OPLOCK_EXCLUSIVE
) {
337 pCifsInode
->clientCanCacheAll
= true;
338 pCifsInode
->clientCanCacheRead
= true;
339 cFYI(1, ("Exclusive Oplock inode %p",
341 } else if ((oplock
& 0xF) == OPLOCK_READ
)
342 pCifsInode
->clientCanCacheRead
= true;
344 write_unlock(&GlobalSMBSeslock
);
354 int cifs_mknod(struct inode
*inode
, struct dentry
*direntry
, int mode
,
359 struct cifs_sb_info
*cifs_sb
;
360 struct cifsTconInfo
*pTcon
;
361 char *full_path
= NULL
;
362 struct inode
*newinode
= NULL
;
364 if (!old_valid_dev(device_number
))
369 cifs_sb
= CIFS_SB(inode
->i_sb
);
370 pTcon
= cifs_sb
->tcon
;
372 full_path
= build_path_from_dentry(direntry
);
373 if (full_path
== NULL
)
375 else if (pTcon
->unix_ext
) {
376 struct cifs_unix_set_info_args args
= {
377 .mode
= mode
& ~current
->fs
->umask
,
378 .ctime
= NO_CHANGE_64
,
379 .atime
= NO_CHANGE_64
,
380 .mtime
= NO_CHANGE_64
,
381 .device
= device_number
,
383 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SET_UID
) {
384 args
.uid
= (__u64
) current_fsuid();
385 args
.gid
= (__u64
) current_fsgid();
387 args
.uid
= NO_CHANGE_64
;
388 args
.gid
= NO_CHANGE_64
;
390 rc
= CIFSSMBUnixSetInfo(xid
, pTcon
, full_path
,
391 &args
, cifs_sb
->local_nls
,
392 cifs_sb
->mnt_cifs_flags
&
393 CIFS_MOUNT_MAP_SPECIAL_CHR
);
396 rc
= cifs_get_inode_info_unix(&newinode
, full_path
,
399 direntry
->d_op
= &cifs_ci_dentry_ops
;
401 direntry
->d_op
= &cifs_dentry_ops
;
403 d_instantiate(direntry
, newinode
);
406 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_UNX_EMUL
) {
411 cFYI(1, ("sfu compat create special file"));
413 buf
= kmalloc(sizeof(FILE_ALL_INFO
), GFP_KERNEL
);
420 rc
= CIFSSMBOpen(xid
, pTcon
, full_path
,
421 FILE_CREATE
, /* fail if exists */
422 GENERIC_WRITE
/* BB would
423 WRITE_OWNER | WRITE_DAC be better? */,
424 /* Create a file and set the
425 file attribute to SYSTEM */
426 CREATE_NOT_DIR
| CREATE_OPTION_SPECIAL
,
427 &fileHandle
, &oplock
, buf
,
429 cifs_sb
->mnt_cifs_flags
&
430 CIFS_MOUNT_MAP_SPECIAL_CHR
);
432 /* BB FIXME - add handling for backlevel servers
433 which need legacy open and check for all
434 calls to SMBOpen for fallback to SMBLeagcyOpen */
436 /* BB Do not bother to decode buf since no
437 local inode yet to put timestamps in,
438 but we can reuse it safely */
439 unsigned int bytes_written
;
440 struct win_dev
*pdev
;
441 pdev
= (struct win_dev
*)buf
;
443 memcpy(pdev
->type
, "IntxCHR", 8);
445 cpu_to_le64(MAJOR(device_number
));
447 cpu_to_le64(MINOR(device_number
));
448 rc
= CIFSSMBWrite(xid
, pTcon
,
450 sizeof(struct win_dev
),
451 0, &bytes_written
, (char *)pdev
,
453 } else if (S_ISBLK(mode
)) {
454 memcpy(pdev
->type
, "IntxBLK", 8);
456 cpu_to_le64(MAJOR(device_number
));
458 cpu_to_le64(MINOR(device_number
));
459 rc
= CIFSSMBWrite(xid
, pTcon
,
461 sizeof(struct win_dev
),
462 0, &bytes_written
, (char *)pdev
,
464 } /* else if(S_ISFIFO */
465 CIFSSMBClose(xid
, pTcon
, fileHandle
);
469 /* add code here to set EAs */
480 cifs_lookup(struct inode
*parent_dir_inode
, struct dentry
*direntry
,
481 struct nameidata
*nd
)
484 int rc
= 0; /* to get around spurious gcc warning, set to zero here */
485 struct cifs_sb_info
*cifs_sb
;
486 struct cifsTconInfo
*pTcon
;
487 struct inode
*newInode
= NULL
;
488 char *full_path
= NULL
;
492 cFYI(1, ("parent inode = 0x%p name is: %s and dentry = 0x%p",
493 parent_dir_inode
, direntry
->d_name
.name
, direntry
));
495 /* check whether path exists */
497 cifs_sb
= CIFS_SB(parent_dir_inode
->i_sb
);
498 pTcon
= cifs_sb
->tcon
;
501 * Don't allow the separator character in a path component.
502 * The VFS will not allow "/", but "\" is allowed by posix.
504 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_POSIX_PATHS
)) {
506 for (i
= 0; i
< direntry
->d_name
.len
; i
++)
507 if (direntry
->d_name
.name
[i
] == '\\') {
508 cFYI(1, ("Invalid file name"));
510 return ERR_PTR(-EINVAL
);
514 /* can not grab the rename sem here since it would
515 deadlock in the cases (beginning of sys_rename itself)
516 in which we already have the sb rename sem */
517 full_path
= build_path_from_dentry(direntry
);
518 if (full_path
== NULL
) {
520 return ERR_PTR(-ENOMEM
);
523 if (direntry
->d_inode
!= NULL
) {
524 cFYI(1, ("non-NULL inode in lookup"));
526 cFYI(1, ("NULL inode in lookup"));
528 cFYI(1, ("Full path: %s inode = 0x%p", full_path
, direntry
->d_inode
));
531 rc
= cifs_get_inode_info_unix(&newInode
, full_path
,
532 parent_dir_inode
->i_sb
, xid
);
534 rc
= cifs_get_inode_info(&newInode
, full_path
, NULL
,
535 parent_dir_inode
->i_sb
, xid
, NULL
);
537 if ((rc
== 0) && (newInode
!= NULL
)) {
539 direntry
->d_op
= &cifs_ci_dentry_ops
;
541 direntry
->d_op
= &cifs_dentry_ops
;
542 d_add(direntry
, newInode
);
544 /* since paths are not looked up by component - the parent
545 directories are presumed to be good here */
546 renew_parental_timestamps(direntry
);
548 } else if (rc
== -ENOENT
) {
550 direntry
->d_time
= jiffies
;
552 direntry
->d_op
= &cifs_ci_dentry_ops
;
554 direntry
->d_op
= &cifs_dentry_ops
;
555 d_add(direntry
, NULL
);
556 /* if it was once a directory (but how can we tell?) we could do
557 shrink_dcache_parent(direntry); */
558 } else if (rc
!= -EACCES
) {
559 cERROR(1, ("Unexpected lookup error %d", rc
));
560 /* We special case check for Access Denied - since that
561 is a common return code */
570 cifs_d_revalidate(struct dentry
*direntry
, struct nameidata
*nd
)
574 if (direntry
->d_inode
) {
575 if (cifs_revalidate(direntry
))
578 cFYI(1, ("neg dentry 0x%p name = %s",
579 direntry
, direntry
->d_name
.name
));
580 if (time_after(jiffies
, direntry
->d_time
+ HZ
) ||
581 !lookupCacheEnabled
) {
590 /* static int cifs_d_delete(struct dentry *direntry)
594 cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
599 struct dentry_operations cifs_dentry_ops
= {
600 .d_revalidate
= cifs_d_revalidate
,
601 /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
604 static int cifs_ci_hash(struct dentry
*dentry
, struct qstr
*q
)
606 struct nls_table
*codepage
= CIFS_SB(dentry
->d_inode
->i_sb
)->local_nls
;
610 hash
= init_name_hash();
611 for (i
= 0; i
< q
->len
; i
++)
612 hash
= partial_name_hash(nls_tolower(codepage
, q
->name
[i
]),
614 q
->hash
= end_name_hash(hash
);
619 static int cifs_ci_compare(struct dentry
*dentry
, struct qstr
*a
,
622 struct nls_table
*codepage
= CIFS_SB(dentry
->d_inode
->i_sb
)->local_nls
;
624 if ((a
->len
== b
->len
) &&
625 (nls_strnicmp(codepage
, a
->name
, b
->name
, a
->len
) == 0)) {
627 * To preserve case, don't let an existing negative dentry's
628 * case take precedence. If a is not a negative dentry, this
629 * should have no side effects
631 memcpy((void *)a
->name
, b
->name
, a
->len
);
637 struct dentry_operations cifs_ci_dentry_ops
= {
638 .d_revalidate
= cifs_d_revalidate
,
639 .d_hash
= cifs_ci_hash
,
640 .d_compare
= cifs_ci_compare
,