Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 * |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 46 | build_path_from_dentry(struct dentry *direntry, const struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 77 | full_path[namelen] = CIFS_DIR_SEP(cifs_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 104 | /* char * build_wildcard_path_from_dentry(struct dentry *direntry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if(full_path == NULL) |
| 107 | return full_path; |
| 108 | |
| 109 | full_path[namelen] = '\\'; |
| 110 | full_path[namelen+1] = '*'; |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 111 | full_path[namelen+2] = 0; |
| 112 | BB remove above eight lines BB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 141 | full_path = build_path_from_dentry(direntry, cifs_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 185 | &fileHandle, &oplock, buf, cifs_sb->local_nls, |
| 186 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 199 | cifs_sb->local_nls, |
| 200 | cifs_sb->mnt_cifs_flags & |
| 201 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } else { |
| 203 | CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, |
| 204 | (__u64)-1, |
| 205 | (__u64)-1, |
| 206 | 0 /* dev */, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 207 | cifs_sb->local_nls, |
| 208 | cifs_sb->mnt_cifs_flags & |
| 209 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | else { |
Steve French | d7245c2 | 2005-07-14 18:25:12 -0500 | [diff] [blame] | 212 | /* BB implement mode setting via Windows security descriptors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Steve French | 4a6d87f | 2005-08-13 08:15:54 -0700 | [diff] [blame] | 229 | cFYI(1, |
| 230 | ("Create worked but get_inode_info failed rc = %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | rc)); |
| 232 | } else { |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 233 | if (pTcon->nocase) |
| 234 | direntry->d_op = &cifs_ci_dentry_ops; |
| 235 | else |
| 236 | direntry->d_op = &cifs_dentry_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | d_instantiate(direntry, newinode); |
| 238 | } |
| 239 | if((nd->flags & LOOKUP_OPEN) == FALSE) { |
| 240 | /* mknod case - do not leave file open */ |
| 241 | CIFSSMBClose(xid, pTcon, fileHandle); |
| 242 | } else if(newinode) { |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 243 | pCifsFile = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | kmalloc(sizeof (struct cifsFileInfo), GFP_KERNEL); |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 245 | |
| 246 | if(pCifsFile == NULL) |
| 247 | goto cifs_create_out; |
| 248 | memset((char *)pCifsFile, 0, |
| 249 | sizeof (struct cifsFileInfo)); |
| 250 | pCifsFile->netfid = fileHandle; |
| 251 | pCifsFile->pid = current->tgid; |
| 252 | pCifsFile->pInode = newinode; |
| 253 | pCifsFile->invalidHandle = FALSE; |
| 254 | pCifsFile->closePend = FALSE; |
| 255 | init_MUTEX(&pCifsFile->fh_sem); |
| 256 | /* set the following in open now |
| 257 | pCifsFile->pfile = file; */ |
| 258 | write_lock(&GlobalSMBSeslock); |
| 259 | list_add(&pCifsFile->tlist,&pTcon->openFileList); |
| 260 | pCifsInode = CIFS_I(newinode); |
| 261 | if(pCifsInode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | /* if readable file instance put first in list*/ |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 263 | if (write_only == TRUE) { |
| 264 | list_add_tail(&pCifsFile->flist, |
| 265 | &pCifsInode->openFileList); |
| 266 | } else { |
| 267 | list_add(&pCifsFile->flist, |
| 268 | &pCifsInode->openFileList); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 270 | if((oplock & 0xF) == OPLOCK_EXCLUSIVE) { |
| 271 | pCifsInode->clientCanCacheAll = TRUE; |
| 272 | pCifsInode->clientCanCacheRead = TRUE; |
| 273 | cFYI(1,("Exclusive Oplock for inode %p", |
| 274 | newinode)); |
| 275 | } else if((oplock & 0xF) == OPLOCK_READ) |
| 276 | pCifsInode->clientCanCacheRead = TRUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 278 | write_unlock(&GlobalSMBSeslock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 281 | cifs_create_out: |
| 282 | kfree(buf); |
| 283 | kfree(full_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | FreeXid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | return rc; |
| 286 | } |
| 287 | |
| 288 | int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, dev_t device_number) |
| 289 | { |
| 290 | int rc = -EPERM; |
| 291 | int xid; |
| 292 | struct cifs_sb_info *cifs_sb; |
| 293 | struct cifsTconInfo *pTcon; |
| 294 | char *full_path = NULL; |
| 295 | struct inode * newinode = NULL; |
| 296 | |
| 297 | if (!old_valid_dev(device_number)) |
| 298 | return -EINVAL; |
| 299 | |
| 300 | xid = GetXid(); |
| 301 | |
| 302 | cifs_sb = CIFS_SB(inode->i_sb); |
| 303 | pTcon = cifs_sb->tcon; |
| 304 | |
| 305 | down(&direntry->d_sb->s_vfs_rename_sem); |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 306 | full_path = build_path_from_dentry(direntry, cifs_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | up(&direntry->d_sb->s_vfs_rename_sem); |
| 308 | if(full_path == NULL) |
| 309 | rc = -ENOMEM; |
Steve French | 4a6d87f | 2005-08-13 08:15:54 -0700 | [diff] [blame] | 310 | else if (pTcon->ses->capabilities & CAP_UNIX) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
| 312 | rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, |
| 313 | mode,(__u64)current->euid,(__u64)current->egid, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 314 | device_number, cifs_sb->local_nls, |
| 315 | cifs_sb->mnt_cifs_flags & |
| 316 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } else { |
| 318 | rc = CIFSSMBUnixSetPerms(xid, pTcon, |
| 319 | full_path, mode, (__u64)-1, (__u64)-1, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 320 | device_number, cifs_sb->local_nls, |
| 321 | cifs_sb->mnt_cifs_flags & |
| 322 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | if(!rc) { |
| 326 | rc = cifs_get_inode_info_unix(&newinode, full_path, |
| 327 | inode->i_sb,xid); |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 328 | if (pTcon->nocase) |
| 329 | direntry->d_op = &cifs_ci_dentry_ops; |
| 330 | else |
| 331 | direntry->d_op = &cifs_dentry_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if(rc == 0) |
| 333 | d_instantiate(direntry, newinode); |
| 334 | } |
Steve French | d7245c2 | 2005-07-14 18:25:12 -0500 | [diff] [blame] | 335 | } else { |
Steve French | eda3c029 | 2005-07-21 15:20:28 -0700 | [diff] [blame] | 336 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) { |
| 337 | int oplock = 0; |
| 338 | u16 fileHandle; |
| 339 | FILE_ALL_INFO * buf; |
Steve French | d7245c2 | 2005-07-14 18:25:12 -0500 | [diff] [blame] | 340 | |
| 341 | cFYI(1,("sfu compat create special file")); |
Steve French | d7245c2 | 2005-07-14 18:25:12 -0500 | [diff] [blame] | 342 | |
Steve French | eda3c029 | 2005-07-21 15:20:28 -0700 | [diff] [blame] | 343 | buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL); |
| 344 | if(buf == NULL) { |
| 345 | kfree(full_path); |
| 346 | FreeXid(xid); |
| 347 | return -ENOMEM; |
| 348 | } |
| 349 | |
| 350 | rc = CIFSSMBOpen(xid, pTcon, full_path, |
| 351 | FILE_CREATE, /* fail if exists */ |
| 352 | GENERIC_WRITE /* BB would |
| 353 | WRITE_OWNER | WRITE_DAC be better? */, |
| 354 | /* Create a file and set the |
| 355 | file attribute to SYSTEM */ |
| 356 | CREATE_NOT_DIR | CREATE_OPTION_SPECIAL, |
| 357 | &fileHandle, &oplock, buf, |
| 358 | cifs_sb->local_nls, |
| 359 | cifs_sb->mnt_cifs_flags & |
| 360 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 361 | |
| 362 | if(!rc) { |
| 363 | /* BB Do not bother to decode buf since no |
| 364 | local inode yet to put timestamps in */ |
| 365 | CIFSSMBClose(xid, pTcon, fileHandle); |
| 366 | d_drop(direntry); |
| 367 | } |
| 368 | kfree(buf); |
Steve French | d7245c2 | 2005-07-14 18:25:12 -0500 | [diff] [blame] | 369 | /* add code here to set EAs */ |
| 370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 373 | kfree(full_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | FreeXid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | return rc; |
| 376 | } |
| 377 | |
| 378 | |
| 379 | struct dentry * |
| 380 | cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd) |
| 381 | { |
| 382 | int xid; |
| 383 | int rc = 0; /* to get around spurious gcc warning, set to zero here */ |
| 384 | struct cifs_sb_info *cifs_sb; |
| 385 | struct cifsTconInfo *pTcon; |
| 386 | struct inode *newInode = NULL; |
| 387 | char *full_path = NULL; |
| 388 | |
| 389 | xid = GetXid(); |
| 390 | |
| 391 | cFYI(1, |
| 392 | (" parent inode = 0x%p name is: %s and dentry = 0x%p", |
| 393 | parent_dir_inode, direntry->d_name.name, direntry)); |
| 394 | |
| 395 | /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */ |
| 396 | |
| 397 | /* check whether path exists */ |
| 398 | |
| 399 | cifs_sb = CIFS_SB(parent_dir_inode->i_sb); |
| 400 | pTcon = cifs_sb->tcon; |
| 401 | |
| 402 | /* can not grab the rename sem here since it would |
| 403 | deadlock in the cases (beginning of sys_rename itself) |
| 404 | in which we already have the sb rename sem */ |
Jeremy Allison | ac67055 | 2005-06-22 17:26:35 -0700 | [diff] [blame] | 405 | full_path = build_path_from_dentry(direntry, cifs_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | if(full_path == NULL) { |
| 407 | FreeXid(xid); |
| 408 | return ERR_PTR(-ENOMEM); |
| 409 | } |
| 410 | |
| 411 | if (direntry->d_inode != NULL) { |
| 412 | cFYI(1, (" non-NULL inode in lookup")); |
| 413 | } else { |
| 414 | cFYI(1, (" NULL inode in lookup")); |
| 415 | } |
| 416 | cFYI(1, |
| 417 | (" Full path: %s inode = 0x%p", full_path, direntry->d_inode)); |
| 418 | |
| 419 | if (pTcon->ses->capabilities & CAP_UNIX) |
| 420 | rc = cifs_get_inode_info_unix(&newInode, full_path, |
| 421 | parent_dir_inode->i_sb,xid); |
| 422 | else |
| 423 | rc = cifs_get_inode_info(&newInode, full_path, NULL, |
| 424 | parent_dir_inode->i_sb,xid); |
| 425 | |
| 426 | if ((rc == 0) && (newInode != NULL)) { |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 427 | if (pTcon->nocase) |
| 428 | direntry->d_op = &cifs_ci_dentry_ops; |
| 429 | else |
| 430 | direntry->d_op = &cifs_dentry_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | d_add(direntry, newInode); |
| 432 | |
| 433 | /* since paths are not looked up by component - the parent directories are presumed to be good here */ |
| 434 | renew_parental_timestamps(direntry); |
| 435 | |
| 436 | } else if (rc == -ENOENT) { |
| 437 | rc = 0; |
| 438 | d_add(direntry, NULL); |
| 439 | } else { |
Steve French | b2aeb9d | 2005-05-17 13:16:18 -0500 | [diff] [blame] | 440 | cERROR(1,("Error 0x%x on cifs_get_inode_info in lookup of %s", |
| 441 | rc,full_path)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | /* BB special case check for Access Denied - watch security |
| 443 | exposure of returning dir info implicitly via different rc |
| 444 | if file exists or not but no access BB */ |
| 445 | } |
| 446 | |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 447 | kfree(full_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | FreeXid(xid); |
| 449 | return ERR_PTR(rc); |
| 450 | } |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | static int |
| 453 | cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd) |
| 454 | { |
| 455 | int isValid = 1; |
| 456 | |
| 457 | /* lock_kernel(); *//* surely we do not want to lock the kernel for a whole network round trip which could take seconds */ |
| 458 | |
| 459 | if (direntry->d_inode) { |
| 460 | if (cifs_revalidate(direntry)) { |
| 461 | /* unlock_kernel(); */ |
| 462 | return 0; |
| 463 | } |
| 464 | } else { |
| 465 | cFYI(1, |
| 466 | ("In cifs_d_revalidate with no inode but name = %s and dentry 0x%p", |
| 467 | direntry->d_name.name, direntry)); |
| 468 | } |
| 469 | |
| 470 | /* unlock_kernel(); */ |
| 471 | |
| 472 | return isValid; |
| 473 | } |
| 474 | |
| 475 | /* static int cifs_d_delete(struct dentry *direntry) |
| 476 | { |
| 477 | int rc = 0; |
| 478 | |
| 479 | cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name)); |
| 480 | |
| 481 | return rc; |
| 482 | } */ |
| 483 | |
| 484 | struct dentry_operations cifs_dentry_ops = { |
| 485 | .d_revalidate = cifs_d_revalidate, |
| 486 | /* d_delete: cifs_d_delete, *//* not needed except for debugging */ |
| 487 | /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */ |
| 488 | }; |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 489 | |
| 490 | static int cifs_ci_hash(struct dentry *dentry, struct qstr *q) |
| 491 | { |
| 492 | struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls; |
| 493 | unsigned long hash; |
| 494 | int i; |
| 495 | |
| 496 | hash = init_name_hash(); |
| 497 | for (i = 0; i < q->len; i++) |
| 498 | hash = partial_name_hash(nls_tolower(codepage, q->name[i]), |
| 499 | hash); |
| 500 | q->hash = end_name_hash(hash); |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static int cifs_ci_compare(struct dentry *dentry, struct qstr *a, |
| 506 | struct qstr *b) |
| 507 | { |
| 508 | struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls; |
| 509 | |
| 510 | if ((a->len == b->len) && |
| 511 | (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) { |
| 512 | /* |
| 513 | * To preserve case, don't let an existing negative dentry's |
| 514 | * case take precedence. If a is not a negative dentry, this |
| 515 | * should have no side effects |
| 516 | */ |
| 517 | memcpy((unsigned char *)a->name, b->name, a->len); |
| 518 | return 0; |
| 519 | } |
| 520 | return 1; |
| 521 | } |
| 522 | |
| 523 | struct dentry_operations cifs_ci_dentry_ops = { |
| 524 | .d_revalidate = cifs_d_revalidate, |
| 525 | .d_hash = cifs_ci_hash, |
| 526 | .d_compare = cifs_ci_compare, |
| 527 | }; |