Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/cifs/dir.c | |
3 | * | |
4 | * vfs operations that deal with dentries | |
5fdae1f6 | 5 | * |
ad7a2926 | 6 | * Copyright (C) International Business Machines Corp., 2002,2008 |
1da177e4 LT |
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 | ||
99ee4dbd | 34 | static void |
1da177e4 LT |
35 | renew_parental_timestamps(struct dentry *direntry) |
36 | { | |
5fdae1f6 SF |
37 | /* BB check if there is a way to get the kernel to do this or if we |
38 | really need this */ | |
1da177e4 LT |
39 | do { |
40 | direntry->d_time = jiffies; | |
41 | direntry = direntry->d_parent; | |
5fdae1f6 | 42 | } while (!IS_ROOT(direntry)); |
1da177e4 LT |
43 | } |
44 | ||
45 | /* Note: caller must free return buffer */ | |
46 | char * | |
47 | build_path_from_dentry(struct dentry *direntry) | |
48 | { | |
49 | struct dentry *temp; | |
2fe87f02 SF |
50 | int namelen; |
51 | int pplen; | |
646dd539 | 52 | int dfsplen; |
1da177e4 | 53 | char *full_path; |
88274815 | 54 | char dirsep; |
646dd539 | 55 | struct cifs_sb_info *cifs_sb; |
1da177e4 | 56 | |
5fdae1f6 | 57 | if (direntry == NULL) |
1da177e4 LT |
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 */ | |
61 | ||
646dd539 SF |
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); | |
67 | else | |
68 | dfsplen = 0; | |
1da177e4 | 69 | cifs_bp_rename_retry: |
646dd539 | 70 | namelen = pplen + dfsplen; |
1da177e4 LT |
71 | for (temp = direntry; !IS_ROOT(temp);) { |
72 | namelen += (1 + temp->d_name.len); | |
73 | temp = temp->d_parent; | |
5fdae1f6 SF |
74 | if (temp == NULL) { |
75 | cERROR(1, ("corrupt dentry")); | |
1da177e4 LT |
76 | return NULL; |
77 | } | |
78 | } | |
79 | ||
80 | full_path = kmalloc(namelen+1, GFP_KERNEL); | |
5fdae1f6 | 81 | if (full_path == NULL) |
1da177e4 LT |
82 | return full_path; |
83 | full_path[namelen] = 0; /* trailing null */ | |
1da177e4 LT |
84 | for (temp = direntry; !IS_ROOT(temp);) { |
85 | namelen -= 1 + temp->d_name.len; | |
86 | if (namelen < 0) { | |
87 | break; | |
88 | } else { | |
7f57356b | 89 | full_path[namelen] = dirsep; |
1da177e4 LT |
90 | strncpy(full_path + namelen + 1, temp->d_name.name, |
91 | temp->d_name.len); | |
2fe87f02 | 92 | cFYI(0, ("name: %s", full_path + namelen)); |
1da177e4 LT |
93 | } |
94 | temp = temp->d_parent; | |
5fdae1f6 SF |
95 | if (temp == NULL) { |
96 | cERROR(1, ("corrupt dentry")); | |
1da177e4 LT |
97 | kfree(full_path); |
98 | return NULL; | |
99 | } | |
100 | } | |
646dd539 | 101 | if (namelen != pplen + dfsplen) { |
1da177e4 | 102 | cERROR(1, |
2fe87f02 | 103 | ("did not end path lookup where expected namelen is %d", |
1da177e4 | 104 | namelen)); |
5fdae1f6 | 105 | /* presumably this is only possible if racing with a rename |
1da177e4 LT |
106 | of one of the parent directories (we can not lock the dentries |
107 | above us to prevent this, but retrying should be harmless) */ | |
108 | kfree(full_path); | |
1da177e4 LT |
109 | goto cifs_bp_rename_retry; |
110 | } | |
2fe87f02 SF |
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 */ | |
646dd539 SF |
117 | |
118 | if (dfsplen) { | |
119 | strncpy(full_path, cifs_sb->tcon->treeName, dfsplen); | |
120 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) { | |
121 | int i; | |
122 | for (i = 0; i < dfsplen; i++) { | |
123 | if (full_path[i] == '\\') | |
124 | full_path[i] = '/'; | |
125 | } | |
126 | } | |
127 | } | |
128 | strncpy(full_path + dfsplen, CIFS_SB(direntry->d_sb)->prepath, pplen); | |
1da177e4 LT |
129 | return full_path; |
130 | } | |
131 | ||
f818dd55 SF |
132 | static void setup_cifs_dentry(struct cifsTconInfo *tcon, |
133 | struct dentry *direntry, | |
134 | struct inode *newinode) | |
135 | { | |
136 | if (tcon->nocase) | |
137 | direntry->d_op = &cifs_ci_dentry_ops; | |
138 | else | |
139 | direntry->d_op = &cifs_dentry_ops; | |
140 | d_instantiate(direntry, newinode); | |
141 | } | |
142 | ||
3979877e | 143 | /* Inode operations in similar order to how they appear in Linux file fs.h */ |
1da177e4 LT |
144 | |
145 | int | |
146 | cifs_create(struct inode *inode, struct dentry *direntry, int mode, | |
147 | struct nameidata *nd) | |
148 | { | |
149 | int rc = -ENOENT; | |
150 | int xid; | |
67750fb9 | 151 | int create_options = CREATE_NOT_DIR; |
1da177e4 | 152 | int oplock = 0; |
f818dd55 | 153 | /* BB below access is too much for the mknod to request */ |
1da177e4 LT |
154 | int desiredAccess = GENERIC_READ | GENERIC_WRITE; |
155 | __u16 fileHandle; | |
156 | struct cifs_sb_info *cifs_sb; | |
f818dd55 | 157 | struct cifsTconInfo *tcon; |
1da177e4 | 158 | char *full_path = NULL; |
fb8c4b14 | 159 | FILE_ALL_INFO *buf = NULL; |
1da177e4 | 160 | struct inode *newinode = NULL; |
fb8c4b14 | 161 | struct cifsInodeInfo *pCifsInode; |
1da177e4 | 162 | int disposition = FILE_OVERWRITE_IF; |
4b18f2a9 | 163 | bool write_only = false; |
1da177e4 LT |
164 | |
165 | xid = GetXid(); | |
166 | ||
167 | cifs_sb = CIFS_SB(inode->i_sb); | |
f818dd55 | 168 | tcon = cifs_sb->tcon; |
1da177e4 | 169 | |
1da177e4 | 170 | full_path = build_path_from_dentry(direntry); |
5fdae1f6 | 171 | if (full_path == NULL) { |
1da177e4 LT |
172 | FreeXid(xid); |
173 | return -ENOMEM; | |
174 | } | |
175 | ||
f818dd55 SF |
176 | mode &= ~current->fs->umask; |
177 | ||
5fdae1f6 | 178 | if (nd && (nd->flags & LOOKUP_OPEN)) { |
e08fc045 MS |
179 | int oflags = nd->intent.open.flags; |
180 | ||
181 | desiredAccess = 0; | |
182 | if (oflags & FMODE_READ) | |
183 | desiredAccess |= GENERIC_READ; | |
184 | if (oflags & FMODE_WRITE) { | |
185 | desiredAccess |= GENERIC_WRITE; | |
186 | if (!(oflags & FMODE_READ)) | |
4b18f2a9 | 187 | write_only = true; |
1da177e4 LT |
188 | } |
189 | ||
5fdae1f6 | 190 | if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
1da177e4 | 191 | disposition = FILE_CREATE; |
5fdae1f6 | 192 | else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) |
1da177e4 | 193 | disposition = FILE_OVERWRITE_IF; |
5fdae1f6 | 194 | else if ((oflags & O_CREAT) == O_CREAT) |
1da177e4 | 195 | disposition = FILE_OPEN_IF; |
ad7a2926 | 196 | else |
5fdae1f6 | 197 | cFYI(1, ("Create flag not set in create function")); |
1da177e4 LT |
198 | } |
199 | ||
5fdae1f6 SF |
200 | /* BB add processing to set equivalent of mode - e.g. via CreateX with |
201 | ACLs */ | |
1da177e4 LT |
202 | if (oplockEnabled) |
203 | oplock = REQ_OPLOCK; | |
204 | ||
5fdae1f6 SF |
205 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
206 | if (buf == NULL) { | |
1da177e4 LT |
207 | kfree(full_path); |
208 | FreeXid(xid); | |
209 | return -ENOMEM; | |
210 | } | |
67750fb9 | 211 | |
67750fb9 JL |
212 | /* |
213 | * if we're not using unix extensions, see if we need to set | |
214 | * ATTR_READONLY on the create call | |
215 | */ | |
f818dd55 | 216 | if (!tcon->unix_ext && (mode & S_IWUGO) == 0) |
67750fb9 JL |
217 | create_options |= CREATE_OPTION_READONLY; |
218 | ||
5fdae1f6 | 219 | if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS) |
f818dd55 | 220 | rc = CIFSSMBOpen(xid, tcon, full_path, disposition, |
67750fb9 | 221 | desiredAccess, create_options, |
737b758c SF |
222 | &fileHandle, &oplock, buf, cifs_sb->local_nls, |
223 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
5bafd765 SF |
224 | else |
225 | rc = -EIO; /* no NT SMB support fall into legacy open below */ | |
226 | ||
5fdae1f6 | 227 | if (rc == -EIO) { |
a9d02ad4 | 228 | /* old server, retry the open legacy style */ |
f818dd55 | 229 | rc = SMBLegacyOpen(xid, tcon, full_path, disposition, |
67750fb9 | 230 | desiredAccess, create_options, |
a9d02ad4 SF |
231 | &fileHandle, &oplock, buf, cifs_sb->local_nls, |
232 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
5fdae1f6 | 233 | } |
1da177e4 | 234 | if (rc) { |
26a21b98 | 235 | cFYI(1, ("cifs_create returned 0x%x", rc)); |
1da177e4 LT |
236 | } else { |
237 | /* If Open reported that we actually created a file | |
238 | then we now have to set the mode if possible */ | |
f818dd55 | 239 | if ((tcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) { |
4e1e7fb9 JL |
240 | struct cifs_unix_set_info_args args = { |
241 | .mode = mode, | |
242 | .ctime = NO_CHANGE_64, | |
243 | .atime = NO_CHANGE_64, | |
244 | .mtime = NO_CHANGE_64, | |
245 | .device = 0, | |
246 | }; | |
247 | ||
5fdae1f6 | 248 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
a001e5b5 | 249 | args.uid = (__u64) current_fsuid(); |
95089910 JL |
250 | if (inode->i_mode & S_ISGID) |
251 | args.gid = (__u64) inode->i_gid; | |
252 | else | |
a001e5b5 | 253 | args.gid = (__u64) current_fsgid(); |
1da177e4 | 254 | } else { |
4e1e7fb9 JL |
255 | args.uid = NO_CHANGE_64; |
256 | args.gid = NO_CHANGE_64; | |
1da177e4 | 257 | } |
f818dd55 | 258 | CIFSSMBUnixSetInfo(xid, tcon, full_path, &args, |
4e1e7fb9 JL |
259 | cifs_sb->local_nls, |
260 | cifs_sb->mnt_cifs_flags & | |
261 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
3ce53fc4 | 262 | } else { |
5fdae1f6 SF |
263 | /* BB implement mode setting via Windows security |
264 | descriptors e.g. */ | |
f818dd55 | 265 | /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/ |
5fdae1f6 SF |
266 | |
267 | /* Could set r/o dos attribute if mode & 0222 == 0 */ | |
1da177e4 LT |
268 | } |
269 | ||
c18c842b | 270 | /* server might mask mode so we have to query for it */ |
f818dd55 | 271 | if (tcon->unix_ext) |
1da177e4 | 272 | rc = cifs_get_inode_info_unix(&newinode, full_path, |
5fdae1f6 | 273 | inode->i_sb, xid); |
1da177e4 LT |
274 | else { |
275 | rc = cifs_get_inode_info(&newinode, full_path, | |
8b1327f6 SF |
276 | buf, inode->i_sb, xid, |
277 | &fileHandle); | |
5fdae1f6 | 278 | if (newinode) { |
b0fd30d3 JL |
279 | if (cifs_sb->mnt_cifs_flags & |
280 | CIFS_MOUNT_DYNPERM) | |
281 | newinode->i_mode = mode; | |
5fdae1f6 SF |
282 | if ((oplock & CIFS_CREATE_ACTION) && |
283 | (cifs_sb->mnt_cifs_flags & | |
6473a559 | 284 | CIFS_MOUNT_SET_UID)) { |
a001e5b5 | 285 | newinode->i_uid = current_fsuid(); |
95089910 JL |
286 | if (inode->i_mode & S_ISGID) |
287 | newinode->i_gid = | |
288 | inode->i_gid; | |
289 | else | |
290 | newinode->i_gid = | |
a001e5b5 | 291 | current_fsgid(); |
6473a559 SF |
292 | } |
293 | } | |
1da177e4 LT |
294 | } |
295 | ||
296 | if (rc != 0) { | |
f818dd55 SF |
297 | cFYI(1, ("Create worked, get_inode_info failed rc = %d", |
298 | rc)); | |
299 | } else | |
300 | setup_cifs_dentry(tcon, direntry, newinode); | |
301 | ||
7521a3c5 | 302 | if ((nd == NULL /* nfsd case - nfs srv does not set nd */) || |
4b18f2a9 | 303 | (!(nd->flags & LOOKUP_OPEN))) { |
1da177e4 | 304 | /* mknod case - do not leave file open */ |
f818dd55 | 305 | CIFSSMBClose(xid, tcon, fileHandle); |
5fdae1f6 | 306 | } else if (newinode) { |
f818dd55 | 307 | struct cifsFileInfo *pCifsFile = |
92ad9b93 | 308 | kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); |
221601c3 | 309 | |
5fdae1f6 | 310 | if (pCifsFile == NULL) |
d14537f1 | 311 | goto cifs_create_out; |
d14537f1 SF |
312 | pCifsFile->netfid = fileHandle; |
313 | pCifsFile->pid = current->tgid; | |
314 | pCifsFile->pInode = newinode; | |
4b18f2a9 SF |
315 | pCifsFile->invalidHandle = false; |
316 | pCifsFile->closePend = false; | |
d14537f1 | 317 | init_MUTEX(&pCifsFile->fh_sem); |
796e5661 | 318 | mutex_init(&pCifsFile->lock_mutex); |
e466e487 | 319 | INIT_LIST_HEAD(&pCifsFile->llist); |
5fdae1f6 | 320 | atomic_set(&pCifsFile->wrtPending, 0); |
e466e487 | 321 | |
5fdae1f6 | 322 | /* set the following in open now |
d14537f1 SF |
323 | pCifsFile->pfile = file; */ |
324 | write_lock(&GlobalSMBSeslock); | |
f818dd55 | 325 | list_add(&pCifsFile->tlist, &tcon->openFileList); |
d14537f1 | 326 | pCifsInode = CIFS_I(newinode); |
5fdae1f6 | 327 | if (pCifsInode) { |
1da177e4 | 328 | /* if readable file instance put first in list*/ |
4b18f2a9 | 329 | if (write_only) { |
5fdae1f6 | 330 | list_add_tail(&pCifsFile->flist, |
d14537f1 SF |
331 | &pCifsInode->openFileList); |
332 | } else { | |
333 | list_add(&pCifsFile->flist, | |
334 | &pCifsInode->openFileList); | |
1da177e4 | 335 | } |
5fdae1f6 | 336 | if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) { |
4b18f2a9 SF |
337 | pCifsInode->clientCanCacheAll = true; |
338 | pCifsInode->clientCanCacheRead = true; | |
221601c3 | 339 | cFYI(1, ("Exclusive Oplock inode %p", |
d14537f1 | 340 | newinode)); |
5fdae1f6 | 341 | } else if ((oplock & 0xF) == OPLOCK_READ) |
4b18f2a9 | 342 | pCifsInode->clientCanCacheRead = true; |
1da177e4 | 343 | } |
d14537f1 | 344 | write_unlock(&GlobalSMBSeslock); |
1da177e4 | 345 | } |
5fdae1f6 | 346 | } |
d14537f1 SF |
347 | cifs_create_out: |
348 | kfree(buf); | |
349 | kfree(full_path); | |
1da177e4 | 350 | FreeXid(xid); |
1da177e4 LT |
351 | return rc; |
352 | } | |
353 | ||
5fdae1f6 SF |
354 | int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, |
355 | dev_t device_number) | |
1da177e4 LT |
356 | { |
357 | int rc = -EPERM; | |
358 | int xid; | |
359 | struct cifs_sb_info *cifs_sb; | |
360 | struct cifsTconInfo *pTcon; | |
361 | char *full_path = NULL; | |
fb8c4b14 | 362 | struct inode *newinode = NULL; |
1da177e4 LT |
363 | |
364 | if (!old_valid_dev(device_number)) | |
365 | return -EINVAL; | |
366 | ||
367 | xid = GetXid(); | |
368 | ||
369 | cifs_sb = CIFS_SB(inode->i_sb); | |
370 | pTcon = cifs_sb->tcon; | |
371 | ||
1da177e4 | 372 | full_path = build_path_from_dentry(direntry); |
5fdae1f6 | 373 | if (full_path == NULL) |
1da177e4 | 374 | rc = -ENOMEM; |
c18c842b | 375 | else if (pTcon->unix_ext) { |
4e1e7fb9 JL |
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, | |
382 | }; | |
5fdae1f6 | 383 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
a001e5b5 DH |
384 | args.uid = (__u64) current_fsuid(); |
385 | args.gid = (__u64) current_fsgid(); | |
1da177e4 | 386 | } else { |
4e1e7fb9 JL |
387 | args.uid = NO_CHANGE_64; |
388 | args.gid = NO_CHANGE_64; | |
1da177e4 | 389 | } |
4e1e7fb9 JL |
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); | |
1da177e4 | 394 | |
5fdae1f6 | 395 | if (!rc) { |
1da177e4 | 396 | rc = cifs_get_inode_info_unix(&newinode, full_path, |
5fdae1f6 | 397 | inode->i_sb, xid); |
b92327fe SF |
398 | if (pTcon->nocase) |
399 | direntry->d_op = &cifs_ci_dentry_ops; | |
400 | else | |
401 | direntry->d_op = &cifs_dentry_ops; | |
5fdae1f6 | 402 | if (rc == 0) |
1da177e4 LT |
403 | d_instantiate(direntry, newinode); |
404 | } | |
d7245c2c | 405 | } else { |
5fdae1f6 | 406 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) { |
eda3c029 SF |
407 | int oplock = 0; |
408 | u16 fileHandle; | |
ad7a2926 | 409 | FILE_ALL_INFO *buf; |
d7245c2c | 410 | |
5fdae1f6 | 411 | cFYI(1, ("sfu compat create special file")); |
d7245c2c | 412 | |
5fdae1f6 SF |
413 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
414 | if (buf == NULL) { | |
eda3c029 SF |
415 | kfree(full_path); |
416 | FreeXid(xid); | |
417 | return -ENOMEM; | |
418 | } | |
419 | ||
420 | rc = CIFSSMBOpen(xid, pTcon, full_path, | |
421 | FILE_CREATE, /* fail if exists */ | |
5fdae1f6 | 422 | GENERIC_WRITE /* BB would |
eda3c029 SF |
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, | |
428 | cifs_sb->local_nls, | |
5fdae1f6 | 429 | cifs_sb->mnt_cifs_flags & |
eda3c029 SF |
430 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
431 | ||
5bafd765 SF |
432 | /* BB FIXME - add handling for backlevel servers |
433 | which need legacy open and check for all | |
5fdae1f6 SF |
434 | calls to SMBOpen for fallback to SMBLeagcyOpen */ |
435 | if (!rc) { | |
eda3c029 | 436 | /* BB Do not bother to decode buf since no |
86c96b4b SF |
437 | local inode yet to put timestamps in, |
438 | but we can reuse it safely */ | |
77159b4d | 439 | unsigned int bytes_written; |
86c96b4b SF |
440 | struct win_dev *pdev; |
441 | pdev = (struct win_dev *)buf; | |
5fdae1f6 | 442 | if (S_ISCHR(mode)) { |
86c96b4b SF |
443 | memcpy(pdev->type, "IntxCHR", 8); |
444 | pdev->major = | |
445 | cpu_to_le64(MAJOR(device_number)); | |
5fdae1f6 | 446 | pdev->minor = |
86c96b4b SF |
447 | cpu_to_le64(MINOR(device_number)); |
448 | rc = CIFSSMBWrite(xid, pTcon, | |
449 | fileHandle, | |
450 | sizeof(struct win_dev), | |
451 | 0, &bytes_written, (char *)pdev, | |
452 | NULL, 0); | |
5fdae1f6 | 453 | } else if (S_ISBLK(mode)) { |
86c96b4b SF |
454 | memcpy(pdev->type, "IntxBLK", 8); |
455 | pdev->major = | |
456 | cpu_to_le64(MAJOR(device_number)); | |
457 | pdev->minor = | |
458 | cpu_to_le64(MINOR(device_number)); | |
459 | rc = CIFSSMBWrite(xid, pTcon, | |
460 | fileHandle, | |
461 | sizeof(struct win_dev), | |
462 | 0, &bytes_written, (char *)pdev, | |
463 | NULL, 0); | |
464 | } /* else if(S_ISFIFO */ | |
eda3c029 SF |
465 | CIFSSMBClose(xid, pTcon, fileHandle); |
466 | d_drop(direntry); | |
467 | } | |
468 | kfree(buf); | |
d7245c2c SF |
469 | /* add code here to set EAs */ |
470 | } | |
1da177e4 LT |
471 | } |
472 | ||
d14537f1 | 473 | kfree(full_path); |
1da177e4 | 474 | FreeXid(xid); |
1da177e4 LT |
475 | return rc; |
476 | } | |
477 | ||
478 | ||
479 | struct dentry * | |
5fdae1f6 SF |
480 | cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, |
481 | struct nameidata *nd) | |
1da177e4 LT |
482 | { |
483 | int xid; | |
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; | |
489 | ||
490 | xid = GetXid(); | |
491 | ||
61e74801 | 492 | cFYI(1, ("parent inode = 0x%p name is: %s and dentry = 0x%p", |
1da177e4 LT |
493 | parent_dir_inode, direntry->d_name.name, direntry)); |
494 | ||
1da177e4 LT |
495 | /* check whether path exists */ |
496 | ||
497 | cifs_sb = CIFS_SB(parent_dir_inode->i_sb); | |
498 | pTcon = cifs_sb->tcon; | |
499 | ||
296034f7 SF |
500 | /* |
501 | * Don't allow the separator character in a path component. | |
502 | * The VFS will not allow "/", but "\" is allowed by posix. | |
503 | */ | |
504 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) { | |
505 | int i; | |
506 | for (i = 0; i < direntry->d_name.len; i++) | |
507 | if (direntry->d_name.name[i] == '\\') { | |
508 | cFYI(1, ("Invalid file name")); | |
509 | FreeXid(xid); | |
510 | return ERR_PTR(-EINVAL); | |
511 | } | |
512 | } | |
513 | ||
1da177e4 LT |
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); | |
5fdae1f6 | 518 | if (full_path == NULL) { |
1da177e4 LT |
519 | FreeXid(xid); |
520 | return ERR_PTR(-ENOMEM); | |
521 | } | |
522 | ||
523 | if (direntry->d_inode != NULL) { | |
61e74801 | 524 | cFYI(1, ("non-NULL inode in lookup")); |
1da177e4 | 525 | } else { |
61e74801 | 526 | cFYI(1, ("NULL inode in lookup")); |
1da177e4 | 527 | } |
61e74801 | 528 | cFYI(1, ("Full path: %s inode = 0x%p", full_path, direntry->d_inode)); |
1da177e4 | 529 | |
c18c842b | 530 | if (pTcon->unix_ext) |
1da177e4 | 531 | rc = cifs_get_inode_info_unix(&newInode, full_path, |
5fdae1f6 | 532 | parent_dir_inode->i_sb, xid); |
1da177e4 LT |
533 | else |
534 | rc = cifs_get_inode_info(&newInode, full_path, NULL, | |
8b1327f6 | 535 | parent_dir_inode->i_sb, xid, NULL); |
1da177e4 LT |
536 | |
537 | if ((rc == 0) && (newInode != NULL)) { | |
b92327fe SF |
538 | if (pTcon->nocase) |
539 | direntry->d_op = &cifs_ci_dentry_ops; | |
540 | else | |
541 | direntry->d_op = &cifs_dentry_ops; | |
1da177e4 LT |
542 | d_add(direntry, newInode); |
543 | ||
5fdae1f6 | 544 | /* since paths are not looked up by component - the parent |
3abb9272 | 545 | directories are presumed to be good here */ |
1da177e4 LT |
546 | renew_parental_timestamps(direntry); |
547 | ||
548 | } else if (rc == -ENOENT) { | |
549 | rc = 0; | |
3abb9272 SF |
550 | direntry->d_time = jiffies; |
551 | if (pTcon->nocase) | |
552 | direntry->d_op = &cifs_ci_dentry_ops; | |
553 | else | |
554 | direntry->d_op = &cifs_dentry_ops; | |
1da177e4 | 555 | d_add(direntry, NULL); |
5fdae1f6 SF |
556 | /* if it was once a directory (but how can we tell?) we could do |
557 | shrink_dcache_parent(direntry); */ | |
ed2b9170 SF |
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 */ | |
1da177e4 LT |
562 | } |
563 | ||
d14537f1 | 564 | kfree(full_path); |
1da177e4 LT |
565 | FreeXid(xid); |
566 | return ERR_PTR(rc); | |
567 | } | |
568 | ||
1da177e4 LT |
569 | static int |
570 | cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd) | |
571 | { | |
572 | int isValid = 1; | |
573 | ||
1da177e4 | 574 | if (direntry->d_inode) { |
ad7a2926 | 575 | if (cifs_revalidate(direntry)) |
1da177e4 | 576 | return 0; |
1da177e4 | 577 | } else { |
3abb9272 SF |
578 | cFYI(1, ("neg dentry 0x%p name = %s", |
579 | direntry, direntry->d_name.name)); | |
5fdae1f6 | 580 | if (time_after(jiffies, direntry->d_time + HZ) || |
3abb9272 SF |
581 | !lookupCacheEnabled) { |
582 | d_drop(direntry); | |
583 | isValid = 0; | |
5fdae1f6 | 584 | } |
1da177e4 LT |
585 | } |
586 | ||
1da177e4 LT |
587 | return isValid; |
588 | } | |
589 | ||
590 | /* static int cifs_d_delete(struct dentry *direntry) | |
591 | { | |
592 | int rc = 0; | |
593 | ||
594 | cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name)); | |
595 | ||
596 | return rc; | |
597 | } */ | |
598 | ||
599 | struct dentry_operations cifs_dentry_ops = { | |
600 | .d_revalidate = cifs_d_revalidate, | |
5fdae1f6 | 601 | /* d_delete: cifs_d_delete, */ /* not needed except for debugging */ |
1da177e4 | 602 | }; |
b92327fe SF |
603 | |
604 | static int cifs_ci_hash(struct dentry *dentry, struct qstr *q) | |
605 | { | |
606 | struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls; | |
607 | unsigned long hash; | |
608 | int i; | |
609 | ||
610 | hash = init_name_hash(); | |
611 | for (i = 0; i < q->len; i++) | |
612 | hash = partial_name_hash(nls_tolower(codepage, q->name[i]), | |
613 | hash); | |
614 | q->hash = end_name_hash(hash); | |
615 | ||
616 | return 0; | |
617 | } | |
618 | ||
619 | static int cifs_ci_compare(struct dentry *dentry, struct qstr *a, | |
620 | struct qstr *b) | |
621 | { | |
622 | struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls; | |
623 | ||
624 | if ((a->len == b->len) && | |
625 | (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) { | |
626 | /* | |
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 | |
630 | */ | |
c3291637 | 631 | memcpy((void *)a->name, b->name, a->len); |
b92327fe SF |
632 | return 0; |
633 | } | |
634 | return 1; | |
635 | } | |
636 | ||
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, | |
641 | }; |