HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / cifs / dir.c
blobbe16da31cbccfcf65d1fb1794f2bc212eadf7c9b
1 /*
2 * fs/cifs/dir.c
4 * vfs operations that deal with dentries
6 * Copyright (C) International Business Machines Corp., 2002,2009
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
23 #include <linux/fs.h>
24 #include <linux/stat.h>
25 #include <linux/slab.h>
26 #include <linux/namei.h>
27 #include <linux/mount.h>
28 #include <linux/file.h>
29 #include "cifsfs.h"
30 #include "cifspdu.h"
31 #include "cifsglob.h"
32 #include "cifsproto.h"
33 #include "cifs_debug.h"
34 #include "cifs_fs_sb.h"
35 #include "cifs_unicode.h"
37 static void
38 renew_parental_timestamps(struct dentry *direntry)
40 /* BB check if there is a way to get the kernel to do this or if we
41 really need this */
42 do {
43 direntry->d_time = jiffies;
44 direntry = direntry->d_parent;
45 } while (!IS_ROOT(direntry));
48 char *
49 cifs_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
50 struct cifs_tcon *tcon)
52 int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
53 int dfsplen;
54 char *full_path = NULL;
56 /* if no prefix path, simply set path to the root of share to "" */
57 if (pplen == 0) {
58 full_path = kzalloc(1, GFP_KERNEL);
59 return full_path;
62 if (tcon->Flags & SMB_SHARE_IS_IN_DFS)
63 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
64 else
65 dfsplen = 0;
67 full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
68 if (full_path == NULL)
69 return full_path;
71 if (dfsplen)
72 strncpy(full_path, tcon->treeName, dfsplen);
73 full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb);
74 strncpy(full_path + dfsplen + 1, vol->prepath, pplen);
75 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
76 full_path[dfsplen + pplen] = 0; /* add trailing null */
77 return full_path;
80 /* Note: caller must free return buffer */
81 char *
82 build_path_from_dentry(struct dentry *direntry)
84 struct dentry *temp;
85 int namelen;
86 int dfsplen;
87 int pplen = 0;
88 char *full_path;
89 char dirsep;
90 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
91 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
92 unsigned seq;
94 dirsep = CIFS_DIR_SEP(cifs_sb);
95 if (tcon->Flags & SMB_SHARE_IS_IN_DFS)
96 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
97 else
98 dfsplen = 0;
100 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
101 pplen = cifs_sb->prepath ? strlen(cifs_sb->prepath) + 1 : 0;
103 cifs_bp_rename_retry:
104 namelen = dfsplen + pplen;
105 seq = read_seqbegin(&rename_lock);
106 rcu_read_lock();
107 for (temp = direntry; !IS_ROOT(temp);) {
108 namelen += (1 + temp->d_name.len);
109 temp = temp->d_parent;
110 if (temp == NULL) {
111 cifs_dbg(VFS, "corrupt dentry\n");
112 rcu_read_unlock();
113 return NULL;
116 rcu_read_unlock();
118 full_path = kmalloc(namelen+1, GFP_KERNEL);
119 if (full_path == NULL)
120 return full_path;
121 full_path[namelen] = 0; /* trailing null */
122 rcu_read_lock();
123 for (temp = direntry; !IS_ROOT(temp);) {
124 spin_lock(&temp->d_lock);
125 namelen -= 1 + temp->d_name.len;
126 if (namelen < 0) {
127 spin_unlock(&temp->d_lock);
128 break;
129 } else {
130 full_path[namelen] = dirsep;
131 strncpy(full_path + namelen + 1, temp->d_name.name,
132 temp->d_name.len);
133 cifs_dbg(FYI, "name: %s\n", full_path + namelen);
135 spin_unlock(&temp->d_lock);
136 temp = temp->d_parent;
137 if (temp == NULL) {
138 cifs_dbg(VFS, "corrupt dentry\n");
139 rcu_read_unlock();
140 kfree(full_path);
141 return NULL;
144 rcu_read_unlock();
145 if (namelen != dfsplen + pplen || read_seqretry(&rename_lock, seq)) {
146 cifs_dbg(FYI, "did not end path lookup where expected. namelen=%ddfsplen=%d\n",
147 namelen, dfsplen);
148 /* presumably this is only possible if racing with a rename
149 of one of the parent directories (we can not lock the dentries
150 above us to prevent this, but retrying should be harmless) */
151 kfree(full_path);
152 goto cifs_bp_rename_retry;
154 /* DIR_SEP already set for byte 0 / vs \ but not for
155 subsequent slashes in prepath which currently must
156 be entered the right way - not sure if there is an alternative
157 since the '\' is a valid posix character so we can not switch
158 those safely to '/' if any are found in the middle of the prepath */
159 /* BB test paths to Windows with '/' in the midst of prepath */
161 if (pplen) {
162 int i;
164 cifs_dbg(FYI, "using cifs_sb prepath <%s>\n", cifs_sb->prepath);
165 memcpy(full_path+dfsplen+1, cifs_sb->prepath, pplen-1);
166 full_path[dfsplen] = dirsep;
167 for (i = 0; i < pplen-1; i++)
168 if (full_path[dfsplen+1+i] == '/')
169 full_path[dfsplen+1+i] = CIFS_DIR_SEP(cifs_sb);
172 if (dfsplen) {
173 strncpy(full_path, tcon->treeName, dfsplen);
174 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
175 int i;
176 for (i = 0; i < dfsplen; i++) {
177 if (full_path[i] == '\\')
178 full_path[i] = '/';
182 return full_path;
186 * Don't allow path components longer than the server max.
187 * Don't allow the separator character in a path component.
188 * The VFS will not allow "/", but "\" is allowed by posix.
190 static int
191 check_name(struct dentry *direntry, struct cifs_tcon *tcon)
193 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
194 int i;
196 if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength &&
197 direntry->d_name.len >
198 le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength)))
199 return -ENAMETOOLONG;
201 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
202 for (i = 0; i < direntry->d_name.len; i++) {
203 if (direntry->d_name.name[i] == '\\') {
204 cifs_dbg(FYI, "Invalid file name\n");
205 return -EINVAL;
209 return 0;
213 /* Inode operations in similar order to how they appear in Linux file fs.h */
215 static int
216 cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid,
217 struct tcon_link *tlink, unsigned oflags, umode_t mode,
218 __u32 *oplock, struct cifs_fid *fid)
220 int rc = -ENOENT;
221 int create_options = CREATE_NOT_DIR;
222 int desired_access;
223 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
224 struct cifs_tcon *tcon = tlink_tcon(tlink);
225 char *full_path = NULL;
226 FILE_ALL_INFO *buf = NULL;
227 struct inode *newinode = NULL;
228 int disposition;
229 struct TCP_Server_Info *server = tcon->ses->server;
230 struct cifs_open_parms oparms;
232 *oplock = 0;
233 if (tcon->ses->server->oplocks)
234 *oplock = REQ_OPLOCK;
236 full_path = build_path_from_dentry(direntry);
237 if (full_path == NULL) {
238 rc = -ENOMEM;
239 goto out;
242 if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open &&
243 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
244 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
245 rc = cifs_posix_open(full_path, &newinode, inode->i_sb, mode,
246 oflags, oplock, &fid->netfid, xid);
247 switch (rc) {
248 case 0:
249 if (newinode == NULL) {
250 /* query inode info */
251 goto cifs_create_get_file_info;
254 if (S_ISDIR(newinode->i_mode)) {
255 CIFSSMBClose(xid, tcon, fid->netfid);
256 iput(newinode);
257 rc = -EISDIR;
258 goto out;
261 if (!S_ISREG(newinode->i_mode)) {
263 * The server may allow us to open things like
264 * FIFOs, but the client isn't set up to deal
265 * with that. If it's not a regular file, just
266 * close it and proceed as if it were a normal
267 * lookup.
269 CIFSSMBClose(xid, tcon, fid->netfid);
270 goto cifs_create_get_file_info;
272 /* success, no need to query */
273 goto cifs_create_set_dentry;
275 case -ENOENT:
276 goto cifs_create_get_file_info;
278 case -EIO:
279 case -EINVAL:
281 * EIO could indicate that (posix open) operation is not
282 * supported, despite what server claimed in capability
283 * negotiation.
285 * POSIX open in samba versions 3.3.1 and earlier could
286 * incorrectly fail with invalid parameter.
288 tcon->broken_posix_open = true;
289 break;
291 case -EREMOTE:
292 case -EOPNOTSUPP:
294 * EREMOTE indicates DFS junction, which is not handled
295 * in posix open. If either that or op not supported
296 * returned, follow the normal lookup.
298 break;
300 default:
301 goto out;
304 * fallthrough to retry, using older open call, this is case
305 * where server does not support this SMB level, and falsely
306 * claims capability (also get here for DFS case which should be
307 * rare for path not covered on files)
311 desired_access = 0;
312 if (OPEN_FMODE(oflags) & FMODE_READ)
313 desired_access |= GENERIC_READ; /* is this too little? */
314 if (OPEN_FMODE(oflags) & FMODE_WRITE)
315 desired_access |= GENERIC_WRITE;
317 disposition = FILE_OVERWRITE_IF;
318 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
319 disposition = FILE_CREATE;
320 else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
321 disposition = FILE_OVERWRITE_IF;
322 else if ((oflags & O_CREAT) == O_CREAT)
323 disposition = FILE_OPEN_IF;
324 else
325 cifs_dbg(FYI, "Create flag not set in create function\n");
328 * BB add processing to set equivalent of mode - e.g. via CreateX with
329 * ACLs
332 if (!server->ops->open) {
333 rc = -ENOSYS;
334 goto out;
337 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
338 if (buf == NULL) {
339 rc = -ENOMEM;
340 goto out;
344 * if we're not using unix extensions, see if we need to set
345 * ATTR_READONLY on the create call
347 if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
348 create_options |= CREATE_OPTION_READONLY;
350 if (backup_cred(cifs_sb))
351 create_options |= CREATE_OPEN_BACKUP_INTENT;
353 oparms.tcon = tcon;
354 oparms.cifs_sb = cifs_sb;
355 oparms.desired_access = desired_access;
356 oparms.create_options = create_options;
357 oparms.disposition = disposition;
358 oparms.path = full_path;
359 oparms.fid = fid;
360 oparms.reconnect = false;
362 rc = server->ops->open(xid, &oparms, oplock, buf);
363 if (rc) {
364 cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc);
365 goto out;
369 * If Open reported that we actually created a file then we now have to
370 * set the mode if possible.
372 if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) {
373 struct cifs_unix_set_info_args args = {
374 .mode = mode,
375 .ctime = NO_CHANGE_64,
376 .atime = NO_CHANGE_64,
377 .mtime = NO_CHANGE_64,
378 .device = 0,
381 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
382 args.uid = current_fsuid();
383 if (inode->i_mode & S_ISGID)
384 args.gid = inode->i_gid;
385 else
386 args.gid = current_fsgid();
387 } else {
388 args.uid = INVALID_UID; /* no change */
389 args.gid = INVALID_GID; /* no change */
391 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid,
392 current->tgid);
393 } else {
395 * BB implement mode setting via Windows security
396 * descriptors e.g.
398 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
400 /* Could set r/o dos attribute if mode & 0222 == 0 */
403 cifs_create_get_file_info:
404 /* server might mask mode so we have to query for it */
405 if (tcon->unix_ext)
406 rc = cifs_get_inode_info_unix(&newinode, full_path, inode->i_sb,
407 xid);
408 else {
409 rc = cifs_get_inode_info(&newinode, full_path, buf, inode->i_sb,
410 xid, fid);
411 if (newinode) {
412 if (server->ops->set_lease_key)
413 server->ops->set_lease_key(newinode, fid);
414 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
415 newinode->i_mode = mode;
416 if ((*oplock & CIFS_CREATE_ACTION) &&
417 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
418 newinode->i_uid = current_fsuid();
419 if (inode->i_mode & S_ISGID)
420 newinode->i_gid = inode->i_gid;
421 else
422 newinode->i_gid = current_fsgid();
427 cifs_create_set_dentry:
428 if (rc != 0) {
429 cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n",
430 rc);
431 goto out_err;
434 if (S_ISDIR(newinode->i_mode)) {
435 rc = -EISDIR;
436 goto out_err;
439 d_drop(direntry);
440 d_add(direntry, newinode);
442 out:
443 kfree(buf);
444 kfree(full_path);
445 return rc;
447 out_err:
448 if (server->ops->close)
449 server->ops->close(xid, tcon, fid);
450 if (newinode)
451 iput(newinode);
452 goto out;
456 cifs_atomic_open(struct inode *inode, struct dentry *direntry,
457 struct file *file, unsigned oflags, umode_t mode,
458 int *opened)
460 int rc;
461 unsigned int xid;
462 struct tcon_link *tlink;
463 struct cifs_tcon *tcon;
464 struct TCP_Server_Info *server;
465 struct cifs_fid fid;
466 struct cifs_pending_open open;
467 __u32 oplock;
468 struct cifsFileInfo *file_info;
471 * Posix open is only called (at lookup time) for file create now. For
472 * opens (rather than creates), because we do not know if it is a file
473 * or directory yet, and current Samba no longer allows us to do posix
474 * open on dirs, we could end up wasting an open call on what turns out
475 * to be a dir. For file opens, we wait to call posix open till
476 * cifs_open. It could be added to atomic_open in the future but the
477 * performance tradeoff of the extra network request when EISDIR or
478 * EACCES is returned would have to be weighed against the 50% reduction
479 * in network traffic in the other paths.
481 if (!(oflags & O_CREAT)) {
482 struct dentry *res;
485 * Check for hashed negative dentry. We have already revalidated
486 * the dentry and it is fine. No need to perform another lookup.
488 if (!d_unhashed(direntry))
489 return -ENOENT;
491 res = cifs_lookup(inode, direntry, 0);
492 if (IS_ERR(res))
493 return PTR_ERR(res);
495 return finish_no_open(file, res);
498 xid = get_xid();
500 cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
501 inode, direntry, direntry);
503 tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
504 if (IS_ERR(tlink)) {
505 rc = PTR_ERR(tlink);
506 goto out_free_xid;
509 tcon = tlink_tcon(tlink);
511 rc = check_name(direntry, tcon);
512 if (rc)
513 goto out;
515 server = tcon->ses->server;
517 if (server->ops->new_lease_key)
518 server->ops->new_lease_key(&fid);
520 cifs_add_pending_open(&fid, tlink, &open);
522 rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
523 &oplock, &fid);
525 if (rc) {
526 cifs_del_pending_open(&open);
527 goto out;
530 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
531 *opened |= FILE_CREATED;
533 rc = finish_open(file, direntry, generic_file_open, opened);
534 if (rc) {
535 if (server->ops->close)
536 server->ops->close(xid, tcon, &fid);
537 cifs_del_pending_open(&open);
538 goto out;
541 if (file->f_flags & O_DIRECT &&
542 CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
543 if (CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
544 file->f_op = &cifs_file_direct_nobrl_ops;
545 else
546 file->f_op = &cifs_file_direct_ops;
549 file_info = cifs_new_fileinfo(&fid, file, tlink, oplock);
550 if (file_info == NULL) {
551 if (server->ops->close)
552 server->ops->close(xid, tcon, &fid);
553 cifs_del_pending_open(&open);
554 fput(file);
555 rc = -ENOMEM;
558 out:
559 cifs_put_tlink(tlink);
560 out_free_xid:
561 free_xid(xid);
562 return rc;
565 int cifs_create(struct inode *inode, struct dentry *direntry, umode_t mode,
566 bool excl)
568 int rc;
569 unsigned int xid = get_xid();
571 * BB below access is probably too much for mknod to request
572 * but we have to do query and setpathinfo so requesting
573 * less could fail (unless we want to request getatr and setatr
574 * permissions (only). At least for POSIX we do not have to
575 * request so much.
577 unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
578 struct tcon_link *tlink;
579 struct cifs_tcon *tcon;
580 struct TCP_Server_Info *server;
581 struct cifs_fid fid;
582 __u32 oplock;
584 cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
585 inode, direntry, direntry);
587 tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
588 rc = PTR_ERR(tlink);
589 if (IS_ERR(tlink))
590 goto out_free_xid;
592 tcon = tlink_tcon(tlink);
593 server = tcon->ses->server;
595 if (server->ops->new_lease_key)
596 server->ops->new_lease_key(&fid);
598 rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
599 &oplock, &fid);
600 if (!rc && server->ops->close)
601 server->ops->close(xid, tcon, &fid);
603 cifs_put_tlink(tlink);
604 out_free_xid:
605 free_xid(xid);
606 return rc;
609 int cifs_mknod(struct inode *inode, struct dentry *direntry, umode_t mode,
610 dev_t device_number)
612 int rc = -EPERM;
613 unsigned int xid;
614 int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
615 struct cifs_sb_info *cifs_sb;
616 struct tcon_link *tlink;
617 struct cifs_tcon *tcon;
618 struct cifs_io_parms io_parms;
619 char *full_path = NULL;
620 struct inode *newinode = NULL;
621 __u32 oplock = 0;
622 struct cifs_fid fid;
623 struct cifs_open_parms oparms;
624 FILE_ALL_INFO *buf = NULL;
625 unsigned int bytes_written;
626 struct win_dev *pdev;
627 struct kvec iov[2];
629 if (!old_valid_dev(device_number))
630 return -EINVAL;
632 cifs_sb = CIFS_SB(inode->i_sb);
633 tlink = cifs_sb_tlink(cifs_sb);
634 if (IS_ERR(tlink))
635 return PTR_ERR(tlink);
637 tcon = tlink_tcon(tlink);
639 xid = get_xid();
641 full_path = build_path_from_dentry(direntry);
642 if (full_path == NULL) {
643 rc = -ENOMEM;
644 goto mknod_out;
647 if (tcon->unix_ext) {
648 struct cifs_unix_set_info_args args = {
649 .mode = mode & ~current_umask(),
650 .ctime = NO_CHANGE_64,
651 .atime = NO_CHANGE_64,
652 .mtime = NO_CHANGE_64,
653 .device = device_number,
655 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
656 args.uid = current_fsuid();
657 args.gid = current_fsgid();
658 } else {
659 args.uid = INVALID_UID; /* no change */
660 args.gid = INVALID_GID; /* no change */
662 rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
663 cifs_sb->local_nls,
664 cifs_remap(cifs_sb));
665 if (rc)
666 goto mknod_out;
668 rc = cifs_get_inode_info_unix(&newinode, full_path,
669 inode->i_sb, xid);
671 if (rc == 0)
672 d_instantiate(direntry, newinode);
673 goto mknod_out;
676 if (!S_ISCHR(mode) && !S_ISBLK(mode))
677 goto mknod_out;
679 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
680 goto mknod_out;
683 cifs_dbg(FYI, "sfu compat create special file\n");
685 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
686 if (buf == NULL) {
687 rc = -ENOMEM;
688 goto mknod_out;
691 if (backup_cred(cifs_sb))
692 create_options |= CREATE_OPEN_BACKUP_INTENT;
694 oparms.tcon = tcon;
695 oparms.cifs_sb = cifs_sb;
696 oparms.desired_access = GENERIC_WRITE;
697 oparms.create_options = create_options;
698 oparms.disposition = FILE_CREATE;
699 oparms.path = full_path;
700 oparms.fid = &fid;
701 oparms.reconnect = false;
703 if (tcon->ses->server->oplocks)
704 oplock = REQ_OPLOCK;
705 else
706 oplock = 0;
707 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
708 if (rc)
709 goto mknod_out;
712 * BB Do not bother to decode buf since no local inode yet to put
713 * timestamps in, but we can reuse it safely.
716 pdev = (struct win_dev *)buf;
717 io_parms.pid = current->tgid;
718 io_parms.tcon = tcon;
719 io_parms.offset = 0;
720 io_parms.length = sizeof(struct win_dev);
721 iov[1].iov_base = buf;
722 iov[1].iov_len = sizeof(struct win_dev);
723 if (S_ISCHR(mode)) {
724 memcpy(pdev->type, "IntxCHR", 8);
725 pdev->major = cpu_to_le64(MAJOR(device_number));
726 pdev->minor = cpu_to_le64(MINOR(device_number));
727 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
728 &bytes_written, iov, 1);
729 } else if (S_ISBLK(mode)) {
730 memcpy(pdev->type, "IntxBLK", 8);
731 pdev->major = cpu_to_le64(MAJOR(device_number));
732 pdev->minor = cpu_to_le64(MINOR(device_number));
733 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
734 &bytes_written, iov, 1);
736 tcon->ses->server->ops->close(xid, tcon, &fid);
737 d_drop(direntry);
739 /* FIXME: add code here to set EAs */
741 mknod_out:
742 kfree(full_path);
743 kfree(buf);
744 free_xid(xid);
745 cifs_put_tlink(tlink);
746 return rc;
749 struct dentry *
750 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
751 unsigned int flags)
753 unsigned int xid;
754 int rc = 0; /* to get around spurious gcc warning, set to zero here */
755 struct cifs_sb_info *cifs_sb;
756 struct tcon_link *tlink;
757 struct cifs_tcon *pTcon;
758 struct inode *newInode = NULL;
759 char *full_path = NULL;
761 xid = get_xid();
763 cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
764 parent_dir_inode, direntry, direntry);
766 /* check whether path exists */
768 cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
769 tlink = cifs_sb_tlink(cifs_sb);
770 if (IS_ERR(tlink)) {
771 free_xid(xid);
772 return (struct dentry *)tlink;
774 pTcon = tlink_tcon(tlink);
776 rc = check_name(direntry, pTcon);
777 if (rc)
778 goto lookup_out;
780 /* can not grab the rename sem here since it would
781 deadlock in the cases (beginning of sys_rename itself)
782 in which we already have the sb rename sem */
783 full_path = build_path_from_dentry(direntry);
784 if (full_path == NULL) {
785 rc = -ENOMEM;
786 goto lookup_out;
789 if (d_really_is_positive(direntry)) {
790 cifs_dbg(FYI, "non-NULL inode in lookup\n");
791 } else {
792 cifs_dbg(FYI, "NULL inode in lookup\n");
794 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
795 full_path, d_inode(direntry));
797 if (pTcon->unix_ext) {
798 rc = cifs_get_inode_info_unix(&newInode, full_path,
799 parent_dir_inode->i_sb, xid);
800 } else {
801 rc = cifs_get_inode_info(&newInode, full_path, NULL,
802 parent_dir_inode->i_sb, xid, NULL);
805 if ((rc == 0) && (newInode != NULL)) {
806 d_add(direntry, newInode);
807 /* since paths are not looked up by component - the parent
808 directories are presumed to be good here */
809 renew_parental_timestamps(direntry);
811 } else if (rc == -ENOENT) {
812 rc = 0;
813 direntry->d_time = jiffies;
814 d_add(direntry, NULL);
815 /* if it was once a directory (but how can we tell?) we could do
816 shrink_dcache_parent(direntry); */
817 } else if (rc != -EACCES) {
818 cifs_dbg(FYI, "Unexpected lookup error %d\n", rc);
819 /* We special case check for Access Denied - since that
820 is a common return code */
823 lookup_out:
824 kfree(full_path);
825 cifs_put_tlink(tlink);
826 free_xid(xid);
827 return ERR_PTR(rc);
830 static int
831 cifs_d_revalidate(struct dentry *direntry, unsigned int flags)
833 struct inode *inode;
835 if (flags & LOOKUP_RCU)
836 return -ECHILD;
838 if (d_really_is_positive(direntry)) {
839 inode = d_inode(direntry);
840 if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode)))
841 CIFS_I(inode)->time = 0; /* force reval */
843 if (cifs_revalidate_dentry(direntry))
844 return 0;
845 else {
847 * If the inode wasn't known to be a dfs entry when
848 * the dentry was instantiated, such as when created
849 * via ->readdir(), it needs to be set now since the
850 * attributes will have been updated by
851 * cifs_revalidate_dentry().
853 if (IS_AUTOMOUNT(inode) &&
854 !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
855 spin_lock(&direntry->d_lock);
856 direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
857 spin_unlock(&direntry->d_lock);
860 return 1;
865 * This may be nfsd (or something), anyway, we can't see the
866 * intent of this. So, since this can be for creation, drop it.
868 if (!flags)
869 return 0;
872 * Drop the negative dentry, in order to make sure to use the
873 * case sensitive name which is specified by user if this is
874 * for creation.
876 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
877 return 0;
879 if (time_after(jiffies, direntry->d_time + HZ) || !lookupCacheEnabled)
880 return 0;
882 return 1;
885 /* static int cifs_d_delete(struct dentry *direntry)
887 int rc = 0;
889 cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry);
891 return rc;
892 } */
894 const struct dentry_operations cifs_dentry_ops = {
895 .d_revalidate = cifs_d_revalidate,
896 .d_automount = cifs_dfs_d_automount,
897 /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
900 static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
902 struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
903 unsigned long hash;
904 wchar_t c;
905 int i, charlen;
907 hash = init_name_hash();
908 for (i = 0; i < q->len; i += charlen) {
909 charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
910 /* error out if we can't convert the character */
911 if (unlikely(charlen < 0))
912 return charlen;
913 hash = partial_name_hash(cifs_toupper(c), hash);
915 q->hash = end_name_hash(hash);
917 return 0;
920 static int cifs_ci_compare(const struct dentry *parent, const struct dentry *dentry,
921 unsigned int len, const char *str, const struct qstr *name)
923 struct nls_table *codepage = CIFS_SB(parent->d_sb)->local_nls;
924 wchar_t c1, c2;
925 int i, l1, l2;
928 * We make the assumption here that uppercase characters in the local
929 * codepage are always the same length as their lowercase counterparts.
931 * If that's ever not the case, then this will fail to match it.
933 if (name->len != len)
934 return 1;
936 for (i = 0; i < len; i += l1) {
937 /* Convert characters in both strings to UTF-16. */
938 l1 = codepage->char2uni(&str[i], len - i, &c1);
939 l2 = codepage->char2uni(&name->name[i], name->len - i, &c2);
942 * If we can't convert either character, just declare it to
943 * be 1 byte long and compare the original byte.
945 if (unlikely(l1 < 0 && l2 < 0)) {
946 if (str[i] != name->name[i])
947 return 1;
948 l1 = 1;
949 continue;
953 * Here, we again ass|u|me that upper/lowercase versions of
954 * a character are the same length in the local NLS.
956 if (l1 != l2)
957 return 1;
959 /* Now compare uppercase versions of these characters */
960 if (cifs_toupper(c1) != cifs_toupper(c2))
961 return 1;
964 return 0;
967 const struct dentry_operations cifs_ci_dentry_ops = {
968 .d_revalidate = cifs_d_revalidate,
969 .d_hash = cifs_ci_hash,
970 .d_compare = cifs_ci_compare,
971 .d_automount = cifs_dfs_d_automount,