blob: 66b825ade3e194b9176717275d29ce2731730c4f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/dir.c
3 *
4 * vfs operations that deal with dentries
5 *
Steve French83451872005-12-01 17:12:59 -08006 * Copyright (C) International Business Machines Corp., 2002,2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 *
46build_path_from_dentry(struct dentry *direntry)
47{
48 struct dentry *temp;
Steve French2fe87f02006-09-21 07:02:52 +000049 int namelen;
50 int pplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 char *full_path;
Steve French88274812006-03-09 22:21:45 +000052 char dirsep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 if(direntry == NULL)
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 */
58
Steve French88274812006-03-09 22:21:45 +000059 dirsep = CIFS_DIR_SEP(CIFS_SB(direntry->d_sb));
Steve French2fe87f02006-09-21 07:02:52 +000060 pplen = CIFS_SB(direntry->d_sb)->prepathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061cifs_bp_rename_retry:
Steve French2fe87f02006-09-21 07:02:52 +000062 namelen = pplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 for (temp = direntry; !IS_ROOT(temp);) {
64 namelen += (1 + temp->d_name.len);
65 temp = temp->d_parent;
66 if(temp == NULL) {
67 cERROR(1,("corrupt dentry"));
68 return NULL;
69 }
70 }
71
72 full_path = kmalloc(namelen+1, GFP_KERNEL);
73 if(full_path == NULL)
74 return full_path;
75 full_path[namelen] = 0; /* trailing null */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 for (temp = direntry; !IS_ROOT(temp);) {
77 namelen -= 1 + temp->d_name.len;
78 if (namelen < 0) {
79 break;
80 } else {
Steve French7f573562005-08-30 11:32:14 -070081 full_path[namelen] = dirsep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 strncpy(full_path + namelen + 1, temp->d_name.name,
83 temp->d_name.len);
Steve French2fe87f02006-09-21 07:02:52 +000084 cFYI(0, ("name: %s", full_path + namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 temp = temp->d_parent;
87 if(temp == NULL) {
88 cERROR(1,("corrupt dentry"));
89 kfree(full_path);
90 return NULL;
91 }
92 }
Steve French2fe87f02006-09-21 07:02:52 +000093 if (namelen != pplen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 cERROR(1,
Steve French2fe87f02006-09-21 07:02:52 +000095 ("did not end path lookup where expected namelen is %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 namelen));
Steve French2fe87f02006-09-21 07:02:52 +000097 /* presumably this is only possible if racing with a rename
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 of one of the parent directories (we can not lock the dentries
99 above us to prevent this, but retrying should be harmless) */
100 kfree(full_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 goto cifs_bp_rename_retry;
102 }
Steve French2fe87f02006-09-21 07:02:52 +0000103 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return full_path;
111}
112
Steve French737b7582005-04-28 22:41:06 -0700113/* char * build_wildcard_path_from_dentry(struct dentry *direntry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if(full_path == NULL)
116 return full_path;
117
118 full_path[namelen] = '\\';
119 full_path[namelen+1] = '*';
Steve French737b7582005-04-28 22:41:06 -0700120 full_path[namelen+2] = 0;
121BB remove above eight lines BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Steve French39798772006-05-31 22:40:51 +0000123/* Inode operations in similar order to how they appear in Linux file fs.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125int
126cifs_create(struct inode *inode, struct dentry *direntry, int mode,
127 struct nameidata *nd)
128{
129 int rc = -ENOENT;
130 int xid;
131 int oplock = 0;
132 int desiredAccess = GENERIC_READ | GENERIC_WRITE;
133 __u16 fileHandle;
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;
143
144 xid = GetXid();
145
146 cifs_sb = CIFS_SB(inode->i_sb);
147 pTcon = cifs_sb->tcon;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if(full_path == NULL) {
151 FreeXid(xid);
152 return -ENOMEM;
153 }
154
Miklos Szeredie08fc042005-09-06 15:18:26 -0700155 if(nd && (nd->flags & LOOKUP_OPEN)) {
156 int oflags = nd->intent.open.flags;
157
158 desiredAccess = 0;
159 if (oflags & FMODE_READ)
160 desiredAccess |= GENERIC_READ;
161 if (oflags & FMODE_WRITE) {
162 desiredAccess |= GENERIC_WRITE;
163 if (!(oflags & FMODE_READ))
164 write_only = TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
Miklos Szeredie08fc042005-09-06 15:18:26 -0700167 if((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 disposition = FILE_CREATE;
Miklos Szeredie08fc042005-09-06 15:18:26 -0700169 else if((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 disposition = FILE_OVERWRITE_IF;
Miklos Szeredie08fc042005-09-06 15:18:26 -0700171 else if((oflags & O_CREAT) == O_CREAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 disposition = FILE_OPEN_IF;
173 else {
174 cFYI(1,("Create flag not set in create function"));
175 }
176 }
177
178 /* BB add processing to set equivalent of mode - e.g. via CreateX with ACLs */
179 if (oplockEnabled)
180 oplock = REQ_OPLOCK;
181
182 buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);
183 if(buf == NULL) {
184 kfree(full_path);
185 FreeXid(xid);
186 return -ENOMEM;
187 }
Steve French5bafd762006-06-07 00:18:43 +0000188 if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
189 rc = CIFSSMBOpen(xid, pTcon, full_path, disposition,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 desiredAccess, CREATE_NOT_DIR,
Steve French737b7582005-04-28 22:41:06 -0700191 &fileHandle, &oplock, buf, cifs_sb->local_nls,
192 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French5bafd762006-06-07 00:18:43 +0000193 else
194 rc = -EIO; /* no NT SMB support fall into legacy open below */
195
Steve Frencha9d02ad2005-08-24 23:06:05 -0700196 if(rc == -EIO) {
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);
202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (rc) {
Steve French26a21b92006-05-31 18:05:34 +0000204 cFYI(1, ("cifs_create returned 0x%x", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 } else {
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,
Steve French83451872005-12-01 17:12:59 -0800212 (__u64)current->fsuid,
213 (__u64)current->fsgid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 0 /* dev */,
Steve French737b7582005-04-28 22:41:06 -0700215 cifs_sb->local_nls,
216 cifs_sb->mnt_cifs_flags &
217 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 } else {
219 CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode,
220 (__u64)-1,
221 (__u64)-1,
222 0 /* dev */,
Steve French737b7582005-04-28 22:41:06 -0700223 cifs_sb->local_nls,
224 cifs_sb->mnt_cifs_flags &
225 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 else {
Steve Frenchd7245c22005-07-14 18:25:12 -0500228 /* BB implement mode setting via Windows security descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* eg CIFSSMBWinSetPerms(xid,pTcon,full_path,mode,-1,-1,local_nls);*/
230 /* could set r/o dos attribute if mode & 0222 == 0 */
231 }
232
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,
236 inode->i_sb,xid);
237 else {
238 rc = cifs_get_inode_info(&newinode, full_path,
239 buf, inode->i_sb,xid);
Steve French6473a552005-11-29 20:20:10 -0800240 if(newinode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 newinode->i_mode = mode;
Steve French6473a552005-11-29 20:20:10 -0800242 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;
247 }
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
251 if (rc != 0) {
Steve French4a6d87f2005-08-13 08:15:54 -0700252 cFYI(1,
253 ("Create worked but get_inode_info failed rc = %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 rc));
255 } else {
Steve Frenchb92327f2005-08-22 20:09:43 -0700256 if (pTcon->nocase)
257 direntry->d_op = &cifs_ci_dentry_ops;
258 else
259 direntry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 d_instantiate(direntry, newinode);
261 }
262 if((nd->flags & LOOKUP_OPEN) == FALSE) {
263 /* mknod case - do not leave file open */
264 CIFSSMBClose(xid, pTcon, fileHandle);
265 } else if(newinode) {
Steve Frenchd14537f12005-04-28 22:41:05 -0700266 pCifsFile =
Eric Sesterhenna048d7a2006-02-21 22:33:09 +0000267 kzalloc(sizeof (struct cifsFileInfo), GFP_KERNEL);
Steve Frenchd14537f12005-04-28 22:41:05 -0700268
269 if(pCifsFile == NULL)
270 goto cifs_create_out;
Steve Frenchd14537f12005-04-28 22:41:05 -0700271 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);
Steve Frenche466e482006-08-15 13:07:18 +0000277 init_MUTEX(&pCifsFile->lock_sem);
278 INIT_LIST_HEAD(&pCifsFile->llist);
279 atomic_set(&pCifsFile->wrtPending,0);
280
Steve Frenchd14537f12005-04-28 22:41:05 -0700281 /* 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);
286 if(pCifsInode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* if readable file instance put first in list*/
Steve Frenchd14537f12005-04-28 22:41:05 -0700288 if (write_only == TRUE) {
289 list_add_tail(&pCifsFile->flist,
290 &pCifsInode->openFileList);
291 } else {
292 list_add(&pCifsFile->flist,
293 &pCifsInode->openFileList);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Steve Frenchd14537f12005-04-28 22:41:05 -0700295 if((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
296 pCifsInode->clientCanCacheAll = TRUE;
297 pCifsInode->clientCanCacheRead = TRUE;
298 cFYI(1,("Exclusive Oplock for inode %p",
299 newinode));
300 } else if((oplock & 0xF) == OPLOCK_READ)
301 pCifsInode->clientCanCacheRead = TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Steve Frenchd14537f12005-04-28 22:41:05 -0700303 write_unlock(&GlobalSMBSeslock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 }
Steve Frenchd14537f12005-04-28 22:41:05 -0700306cifs_create_out:
307 kfree(buf);
308 kfree(full_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 FreeXid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return rc;
311}
312
Steve French86c96b42005-11-18 20:25:31 -0800313int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
314 dev_t device_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 int rc = -EPERM;
317 int xid;
318 struct cifs_sb_info *cifs_sb;
319 struct cifsTconInfo *pTcon;
320 char *full_path = NULL;
321 struct inode * newinode = NULL;
322
323 if (!old_valid_dev(device_number))
324 return -EINVAL;
325
326 xid = GetXid();
327
328 cifs_sb = CIFS_SB(inode->i_sb);
329 pTcon = cifs_sb->tcon;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if(full_path == NULL)
333 rc = -ENOMEM;
Steve French4a6d87f2005-08-13 08:15:54 -0700334 else if (pTcon->ses->capabilities & CAP_UNIX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
336 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path,
Steve French83451872005-12-01 17:12:59 -0800337 mode,(__u64)current->fsuid,(__u64)current->fsgid,
Steve French737b7582005-04-28 22:41:06 -0700338 device_number, cifs_sb->local_nls,
339 cifs_sb->mnt_cifs_flags &
340 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 } else {
342 rc = CIFSSMBUnixSetPerms(xid, pTcon,
343 full_path, mode, (__u64)-1, (__u64)-1,
Steve French737b7582005-04-28 22:41:06 -0700344 device_number, cifs_sb->local_nls,
345 cifs_sb->mnt_cifs_flags &
346 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 if(!rc) {
350 rc = cifs_get_inode_info_unix(&newinode, full_path,
351 inode->i_sb,xid);
Steve Frenchb92327f2005-08-22 20:09:43 -0700352 if (pTcon->nocase)
353 direntry->d_op = &cifs_ci_dentry_ops;
354 else
355 direntry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if(rc == 0)
357 d_instantiate(direntry, newinode);
358 }
Steve Frenchd7245c22005-07-14 18:25:12 -0500359 } else {
Steve Frencheda3c022005-07-21 15:20:28 -0700360 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
361 int oplock = 0;
362 u16 fileHandle;
363 FILE_ALL_INFO * buf;
Steve Frenchd7245c22005-07-14 18:25:12 -0500364
365 cFYI(1,("sfu compat create special file"));
Steve Frenchd7245c22005-07-14 18:25:12 -0500366
Steve Frencheda3c022005-07-21 15:20:28 -0700367 buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);
368 if(buf == NULL) {
369 kfree(full_path);
370 FreeXid(xid);
371 return -ENOMEM;
372 }
373
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,
382 cifs_sb->local_nls,
383 cifs_sb->mnt_cifs_flags &
384 CIFS_MOUNT_MAP_SPECIAL_CHR);
385
Steve French5bafd762006-06-07 00:18:43 +0000386 /* BB FIXME - add handling for backlevel servers
387 which need legacy open and check for all
388 calls to SMBOpen for fallback to
389 SMBLeagcyOpen */
Steve Frencheda3c022005-07-21 15:20:28 -0700390 if(!rc) {
391 /* BB Do not bother to decode buf since no
Steve French86c96b42005-11-18 20:25:31 -0800392 local inode yet to put timestamps in,
393 but we can reuse it safely */
394 int bytes_written;
395 struct win_dev *pdev;
396 pdev = (struct win_dev *)buf;
397 if(S_ISCHR(mode)) {
398 memcpy(pdev->type, "IntxCHR", 8);
399 pdev->major =
400 cpu_to_le64(MAJOR(device_number));
401 pdev->minor =
402 cpu_to_le64(MINOR(device_number));
403 rc = CIFSSMBWrite(xid, pTcon,
404 fileHandle,
405 sizeof(struct win_dev),
406 0, &bytes_written, (char *)pdev,
407 NULL, 0);
408 } else if(S_ISBLK(mode)) {
409 memcpy(pdev->type, "IntxBLK", 8);
410 pdev->major =
411 cpu_to_le64(MAJOR(device_number));
412 pdev->minor =
413 cpu_to_le64(MINOR(device_number));
414 rc = CIFSSMBWrite(xid, pTcon,
415 fileHandle,
416 sizeof(struct win_dev),
417 0, &bytes_written, (char *)pdev,
418 NULL, 0);
419 } /* else if(S_ISFIFO */
Steve Frencheda3c022005-07-21 15:20:28 -0700420 CIFSSMBClose(xid, pTcon, fileHandle);
421 d_drop(direntry);
422 }
423 kfree(buf);
Steve Frenchd7245c22005-07-14 18:25:12 -0500424 /* add code here to set EAs */
425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
Steve Frenchd14537f12005-04-28 22:41:05 -0700428 kfree(full_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 FreeXid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return rc;
431}
432
433
434struct dentry *
435cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd)
436{
437 int xid;
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;
443
444 xid = GetXid();
445
446 cFYI(1,
447 (" parent inode = 0x%p name is: %s and dentry = 0x%p",
448 parent_dir_inode, direntry->d_name.name, direntry));
449
450 /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */
451
452 /* check whether path exists */
453
454 cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
455 pTcon = cifs_sb->tcon;
456
Steve French296034f2006-04-21 18:18:37 +0000457 /*
458 * Don't allow the separator character in a path component.
459 * The VFS will not allow "/", but "\" is allowed by posix.
460 */
461 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
462 int i;
463 for (i = 0; i < direntry->d_name.len; i++)
464 if (direntry->d_name.name[i] == '\\') {
465 cFYI(1, ("Invalid file name"));
466 FreeXid(xid);
467 return ERR_PTR(-EINVAL);
468 }
469 }
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* 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) {
476 FreeXid(xid);
477 return ERR_PTR(-ENOMEM);
478 }
479
480 if (direntry->d_inode != NULL) {
481 cFYI(1, (" non-NULL inode in lookup"));
482 } else {
483 cFYI(1, (" NULL inode in lookup"));
484 }
485 cFYI(1,
486 (" Full path: %s inode = 0x%p", full_path, direntry->d_inode));
487
488 if (pTcon->ses->capabilities & CAP_UNIX)
489 rc = cifs_get_inode_info_unix(&newInode, full_path,
490 parent_dir_inode->i_sb,xid);
491 else
492 rc = cifs_get_inode_info(&newInode, full_path, NULL,
493 parent_dir_inode->i_sb,xid);
494
495 if ((rc == 0) && (newInode != NULL)) {
Steve Frenchb92327f2005-08-22 20:09:43 -0700496 if (pTcon->nocase)
497 direntry->d_op = &cifs_ci_dentry_ops;
498 else
499 direntry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 d_add(direntry, newInode);
501
Steve French3abb9272005-11-28 08:16:13 -0800502 /* since paths are not looked up by component - the parent
503 directories are presumed to be good here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 renew_parental_timestamps(direntry);
505
506 } else if (rc == -ENOENT) {
507 rc = 0;
Steve French3abb9272005-11-28 08:16:13 -0800508 direntry->d_time = jiffies;
509 if (pTcon->nocase)
510 direntry->d_op = &cifs_ci_dentry_ops;
511 else
512 direntry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 d_add(direntry, NULL);
Steve French3abb9272005-11-28 08:16:13 -0800514 /* if it was once a directory (but how can we tell?) we could do
515 shrink_dcache_parent(direntry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 } else {
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500517 cERROR(1,("Error 0x%x on cifs_get_inode_info in lookup of %s",
518 rc,full_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* 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 */
522 }
523
Steve Frenchd14537f12005-04-28 22:41:05 -0700524 kfree(full_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 FreeXid(xid);
526 return ERR_PTR(rc);
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529static int
530cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
531{
532 int isValid = 1;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (direntry->d_inode) {
535 if (cifs_revalidate(direntry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537 }
538 } else {
Steve French3abb9272005-11-28 08:16:13 -0800539 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) {
543 d_drop(direntry);
544 isValid = 0;
545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return isValid;
549}
550
551/* static int cifs_d_delete(struct dentry *direntry)
552{
553 int rc = 0;
554
555 cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
556
557 return rc;
558} */
559
560struct 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 */
564};
Steve Frenchb92327f2005-08-22 20:09:43 -0700565
566static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
567{
568 struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
569 unsigned long hash;
570 int i;
571
572 hash = init_name_hash();
573 for (i = 0; i < q->len; i++)
574 hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
575 hash);
576 q->hash = end_name_hash(hash);
577
578 return 0;
579}
580
581static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
582 struct qstr *b)
583{
584 struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
585
586 if ((a->len == b->len) &&
587 (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
588 /*
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
592 */
593 memcpy((unsigned char *)a->name, b->name, a->len);
594 return 0;
595 }
596 return 1;
597}
598
599struct dentry_operations cifs_ci_dentry_ops = {
600 .d_revalidate = cifs_d_revalidate,
601 .d_hash = cifs_ci_hash,
602 .d_compare = cifs_ci_compare,
603};