GNU Linux-libre 4.9.292-gnu1
[releases.git] / fs / cifs / dir.c
1 /*
2  *   fs/cifs/dir.c
3  *
4  *   vfs operations that deal with dentries
5  *
6  *   Copyright (C) International Business Machines  Corp., 2002,2009
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 <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"
36
37 static void
38 renew_parental_timestamps(struct dentry *direntry)
39 {
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                 cifs_set_time(direntry, jiffies);
44                 direntry = direntry->d_parent;
45         } while (!IS_ROOT(direntry));
46 }
47
48 char *
49 cifs_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
50                         struct cifs_tcon *tcon)
51 {
52         int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
53         int dfsplen;
54         char *full_path = NULL;
55
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;
60         }
61
62         if (tcon->Flags & SMB_SHARE_IS_IN_DFS)
63                 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
64         else
65                 dfsplen = 0;
66
67         full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
68         if (full_path == NULL)
69                 return full_path;
70
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;
78 }
79
80 /* Note: caller must free return buffer */
81 char *
82 build_path_from_dentry(struct dentry *direntry)
83 {
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;
93
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;
99
100         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
101                 pplen = cifs_sb->prepath ? strlen(cifs_sb->prepath) + 1 : 0;
102
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;
114                 }
115         }
116         rcu_read_unlock();
117
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);
134                 }
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;
142                 }
143         }
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;
153         }
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 */
160
161         if (pplen) {
162                 int i;
163
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);
170         }
171
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] = '/';
179                         }
180                 }
181         }
182         return full_path;
183 }
184
185 /*
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.
189  */
190 static int
191 check_name(struct dentry *direntry, struct cifs_tcon *tcon)
192 {
193         struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
194         int i;
195
196         if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength &&
197                      direntry->d_name.len >
198                      le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength)))
199                 return -ENAMETOOLONG;
200
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;
206                         }
207                 }
208         }
209         return 0;
210 }
211
212
213 /* Inode operations in similar order to how they appear in Linux file fs.h */
214
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)
219 {
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;
231
232         *oplock = 0;
233         if (tcon->ses->server->oplocks)
234                 *oplock = REQ_OPLOCK;
235
236         full_path = build_path_from_dentry(direntry);
237         if (full_path == NULL) {
238                 rc = -ENOMEM;
239                 goto out;
240         }
241
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;
252                         }
253
254                         if (S_ISDIR(newinode->i_mode)) {
255                                 CIFSSMBClose(xid, tcon, fid->netfid);
256                                 iput(newinode);
257                                 rc = -EISDIR;
258                                 goto out;
259                         }
260
261                         if (!S_ISREG(newinode->i_mode)) {
262                                 /*
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.
268                                  */
269                                 CIFSSMBClose(xid, tcon, fid->netfid);
270                                 goto cifs_create_get_file_info;
271                         }
272                         /* success, no need to query */
273                         goto cifs_create_set_dentry;
274
275                 case -ENOENT:
276                         goto cifs_create_get_file_info;
277
278                 case -EIO:
279                 case -EINVAL:
280                         /*
281                          * EIO could indicate that (posix open) operation is not
282                          * supported, despite what server claimed in capability
283                          * negotiation.
284                          *
285                          * POSIX open in samba versions 3.3.1 and earlier could
286                          * incorrectly fail with invalid parameter.
287                          */
288                         tcon->broken_posix_open = true;
289                         break;
290
291                 case -EREMOTE:
292                 case -EOPNOTSUPP:
293                         /*
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.
297                          */
298                         break;
299
300                 default:
301                         goto out;
302                 }
303                 /*
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)
308                  */
309         }
310
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;
316
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");
326
327         /*
328          * BB add processing to set equivalent of mode - e.g. via CreateX with
329          * ACLs
330          */
331
332         if (!server->ops->open) {
333                 rc = -ENOSYS;
334                 goto out;
335         }
336
337         buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
338         if (buf == NULL) {
339                 rc = -ENOMEM;
340                 goto out;
341         }
342
343         /*
344          * if we're not using unix extensions, see if we need to set
345          * ATTR_READONLY on the create call
346          */
347         if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
348                 create_options |= CREATE_OPTION_READONLY;
349
350         if (backup_cred(cifs_sb))
351                 create_options |= CREATE_OPEN_BACKUP_INTENT;
352
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;
361
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;
366         }
367
368         /*
369          * If Open reported that we actually created a file then we now have to
370          * set the mode if possible.
371          */
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,
379                 };
380
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 */
390                 }
391                 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid,
392                                        current->tgid);
393         } else {
394                 /*
395                  * BB implement mode setting via Windows security
396                  * descriptors e.g.
397                  */
398                 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
399
400                 /* Could set r/o dos attribute if mode & 0222 == 0 */
401         }
402
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();
423                         }
424                 }
425         }
426
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;
432         }
433
434         if (S_ISDIR(newinode->i_mode)) {
435                 rc = -EISDIR;
436                 goto out_err;
437         }
438
439         d_drop(direntry);
440         d_add(direntry, newinode);
441
442 out:
443         kfree(buf);
444         kfree(full_path);
445         return rc;
446
447 out_err:
448         if (server->ops->close)
449                 server->ops->close(xid, tcon, fid);
450         if (newinode)
451                 iput(newinode);
452         goto out;
453 }
454
455 int
456 cifs_atomic_open(struct inode *inode, struct dentry *direntry,
457                  struct file *file, unsigned oflags, umode_t mode,
458                  int *opened)
459 {
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;
469
470         /*
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.
480          */
481         if (!(oflags & O_CREAT)) {
482                 struct dentry *res;
483
484                 /*
485                  * Check for hashed negative dentry. We have already revalidated
486                  * the dentry and it is fine. No need to perform another lookup.
487                  */
488                 if (!d_in_lookup(direntry))
489                         return -ENOENT;
490
491                 res = cifs_lookup(inode, direntry, 0);
492                 if (IS_ERR(res))
493                         return PTR_ERR(res);
494
495                 return finish_no_open(file, res);
496         }
497
498         xid = get_xid();
499
500         cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
501                  inode, direntry, direntry);
502
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;
507         }
508
509         tcon = tlink_tcon(tlink);
510
511         rc = check_name(direntry, tcon);
512         if (rc)
513                 goto out;
514
515         server = tcon->ses->server;
516
517         if (server->ops->new_lease_key)
518                 server->ops->new_lease_key(&fid);
519
520         cifs_add_pending_open(&fid, tlink, &open);
521
522         rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
523                             &oplock, &fid);
524
525         if (rc) {
526                 cifs_del_pending_open(&open);
527                 goto out;
528         }
529
530         if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
531                 *opened |= FILE_CREATED;
532
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;
539         }
540
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;
547                 }
548
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                 rc = -ENOMEM;
555         }
556
557 out:
558         cifs_put_tlink(tlink);
559 out_free_xid:
560         free_xid(xid);
561         return rc;
562 }
563
564 int cifs_create(struct inode *inode, struct dentry *direntry, umode_t mode,
565                 bool excl)
566 {
567         int rc;
568         unsigned int xid = get_xid();
569         /*
570          * BB below access is probably too much for mknod to request
571          *    but we have to do query and setpathinfo so requesting
572          *    less could fail (unless we want to request getatr and setatr
573          *    permissions (only).  At least for POSIX we do not have to
574          *    request so much.
575          */
576         unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
577         struct tcon_link *tlink;
578         struct cifs_tcon *tcon;
579         struct TCP_Server_Info *server;
580         struct cifs_fid fid;
581         __u32 oplock;
582
583         cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
584                  inode, direntry, direntry);
585
586         tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
587         rc = PTR_ERR(tlink);
588         if (IS_ERR(tlink))
589                 goto out_free_xid;
590
591         tcon = tlink_tcon(tlink);
592         server = tcon->ses->server;
593
594         if (server->ops->new_lease_key)
595                 server->ops->new_lease_key(&fid);
596
597         rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
598                             &oplock, &fid);
599         if (!rc && server->ops->close)
600                 server->ops->close(xid, tcon, &fid);
601
602         cifs_put_tlink(tlink);
603 out_free_xid:
604         free_xid(xid);
605         return rc;
606 }
607
608 int cifs_mknod(struct inode *inode, struct dentry *direntry, umode_t mode,
609                 dev_t device_number)
610 {
611         int rc = -EPERM;
612         unsigned int xid;
613         int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
614         struct cifs_sb_info *cifs_sb;
615         struct tcon_link *tlink;
616         struct cifs_tcon *tcon;
617         struct cifs_io_parms io_parms;
618         char *full_path = NULL;
619         struct inode *newinode = NULL;
620         __u32 oplock = 0;
621         struct cifs_fid fid;
622         struct cifs_open_parms oparms;
623         FILE_ALL_INFO *buf = NULL;
624         unsigned int bytes_written;
625         struct win_dev *pdev;
626         struct kvec iov[2];
627
628         if (!old_valid_dev(device_number))
629                 return -EINVAL;
630
631         cifs_sb = CIFS_SB(inode->i_sb);
632         tlink = cifs_sb_tlink(cifs_sb);
633         if (IS_ERR(tlink))
634                 return PTR_ERR(tlink);
635
636         tcon = tlink_tcon(tlink);
637
638         xid = get_xid();
639
640         full_path = build_path_from_dentry(direntry);
641         if (full_path == NULL) {
642                 rc = -ENOMEM;
643                 goto mknod_out;
644         }
645
646         if (tcon->unix_ext) {
647                 struct cifs_unix_set_info_args args = {
648                         .mode   = mode & ~current_umask(),
649                         .ctime  = NO_CHANGE_64,
650                         .atime  = NO_CHANGE_64,
651                         .mtime  = NO_CHANGE_64,
652                         .device = device_number,
653                 };
654                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
655                         args.uid = current_fsuid();
656                         args.gid = current_fsgid();
657                 } else {
658                         args.uid = INVALID_UID; /* no change */
659                         args.gid = INVALID_GID; /* no change */
660                 }
661                 rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
662                                             cifs_sb->local_nls,
663                                             cifs_remap(cifs_sb));
664                 if (rc)
665                         goto mknod_out;
666
667                 rc = cifs_get_inode_info_unix(&newinode, full_path,
668                                                 inode->i_sb, xid);
669
670                 if (rc == 0)
671                         d_instantiate(direntry, newinode);
672                 goto mknod_out;
673         }
674
675         if (!S_ISCHR(mode) && !S_ISBLK(mode))
676                 goto mknod_out;
677
678         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
679                 goto mknod_out;
680
681
682         cifs_dbg(FYI, "sfu compat create special file\n");
683
684         buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
685         if (buf == NULL) {
686                 rc = -ENOMEM;
687                 goto mknod_out;
688         }
689
690         if (backup_cred(cifs_sb))
691                 create_options |= CREATE_OPEN_BACKUP_INTENT;
692
693         oparms.tcon = tcon;
694         oparms.cifs_sb = cifs_sb;
695         oparms.desired_access = GENERIC_WRITE;
696         oparms.create_options = create_options;
697         oparms.disposition = FILE_CREATE;
698         oparms.path = full_path;
699         oparms.fid = &fid;
700         oparms.reconnect = false;
701
702         if (tcon->ses->server->oplocks)
703                 oplock = REQ_OPLOCK;
704         else
705                 oplock = 0;
706         rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
707         if (rc)
708                 goto mknod_out;
709
710         /*
711          * BB Do not bother to decode buf since no local inode yet to put
712          * timestamps in, but we can reuse it safely.
713          */
714
715         pdev = (struct win_dev *)buf;
716         io_parms.pid = current->tgid;
717         io_parms.tcon = tcon;
718         io_parms.offset = 0;
719         io_parms.length = sizeof(struct win_dev);
720         iov[1].iov_base = buf;
721         iov[1].iov_len = sizeof(struct win_dev);
722         if (S_ISCHR(mode)) {
723                 memcpy(pdev->type, "IntxCHR", 8);
724                 pdev->major = cpu_to_le64(MAJOR(device_number));
725                 pdev->minor = cpu_to_le64(MINOR(device_number));
726                 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
727                                                         &bytes_written, iov, 1);
728         } else if (S_ISBLK(mode)) {
729                 memcpy(pdev->type, "IntxBLK", 8);
730                 pdev->major = cpu_to_le64(MAJOR(device_number));
731                 pdev->minor = cpu_to_le64(MINOR(device_number));
732                 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
733                                                         &bytes_written, iov, 1);
734         }
735         tcon->ses->server->ops->close(xid, tcon, &fid);
736         d_drop(direntry);
737
738         /* FIXME: add code here to set EAs */
739
740 mknod_out:
741         kfree(full_path);
742         kfree(buf);
743         free_xid(xid);
744         cifs_put_tlink(tlink);
745         return rc;
746 }
747
748 struct dentry *
749 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
750             unsigned int flags)
751 {
752         unsigned int xid;
753         int rc = 0; /* to get around spurious gcc warning, set to zero here */
754         struct cifs_sb_info *cifs_sb;
755         struct tcon_link *tlink;
756         struct cifs_tcon *pTcon;
757         struct inode *newInode = NULL;
758         char *full_path = NULL;
759
760         xid = get_xid();
761
762         cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
763                  parent_dir_inode, direntry, direntry);
764
765         /* check whether path exists */
766
767         cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
768         tlink = cifs_sb_tlink(cifs_sb);
769         if (IS_ERR(tlink)) {
770                 free_xid(xid);
771                 return (struct dentry *)tlink;
772         }
773         pTcon = tlink_tcon(tlink);
774
775         rc = check_name(direntry, pTcon);
776         if (rc)
777                 goto lookup_out;
778
779         /* can not grab the rename sem here since it would
780         deadlock in the cases (beginning of sys_rename itself)
781         in which we already have the sb rename sem */
782         full_path = build_path_from_dentry(direntry);
783         if (full_path == NULL) {
784                 rc = -ENOMEM;
785                 goto lookup_out;
786         }
787
788         if (d_really_is_positive(direntry)) {
789                 cifs_dbg(FYI, "non-NULL inode in lookup\n");
790         } else {
791                 cifs_dbg(FYI, "NULL inode in lookup\n");
792         }
793         cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
794                  full_path, d_inode(direntry));
795
796         if (pTcon->unix_ext) {
797                 rc = cifs_get_inode_info_unix(&newInode, full_path,
798                                               parent_dir_inode->i_sb, xid);
799         } else {
800                 rc = cifs_get_inode_info(&newInode, full_path, NULL,
801                                 parent_dir_inode->i_sb, xid, NULL);
802         }
803
804         if ((rc == 0) && (newInode != NULL)) {
805                 d_add(direntry, newInode);
806                 /* since paths are not looked up by component - the parent
807                    directories are presumed to be good here */
808                 renew_parental_timestamps(direntry);
809
810         } else if (rc == -ENOENT) {
811                 rc = 0;
812                 cifs_set_time(direntry, jiffies);
813                 d_add(direntry, NULL);
814         /*      if it was once a directory (but how can we tell?) we could do
815                 shrink_dcache_parent(direntry); */
816         } else if (rc != -EACCES) {
817                 cifs_dbg(FYI, "Unexpected lookup error %d\n", rc);
818                 /* We special case check for Access Denied - since that
819                 is a common return code */
820         }
821
822 lookup_out:
823         kfree(full_path);
824         cifs_put_tlink(tlink);
825         free_xid(xid);
826         return ERR_PTR(rc);
827 }
828
829 static int
830 cifs_d_revalidate(struct dentry *direntry, unsigned int flags)
831 {
832         struct inode *inode;
833         int rc;
834
835         if (flags & LOOKUP_RCU)
836                 return -ECHILD;
837
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 */
842
843                 rc = cifs_revalidate_dentry(direntry);
844                 if (rc) {
845                         cifs_dbg(FYI, "cifs_revalidate_dentry failed with rc=%d", rc);
846                         switch (rc) {
847                         case -ENOENT:
848                         case -ESTALE:
849                                 /*
850                                  * Those errors mean the dentry is invalid
851                                  * (file was deleted or recreated)
852                                  */
853                                 return 0;
854                         default:
855                                 /*
856                                  * Otherwise some unexpected error happened
857                                  * report it as-is to VFS layer
858                                  */
859                                 return rc;
860                         }
861                 }
862                 else {
863                         /*
864                          * If the inode wasn't known to be a dfs entry when
865                          * the dentry was instantiated, such as when created
866                          * via ->readdir(), it needs to be set now since the
867                          * attributes will have been updated by
868                          * cifs_revalidate_dentry().
869                          */
870                         if (IS_AUTOMOUNT(inode) &&
871                            !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
872                                 spin_lock(&direntry->d_lock);
873                                 direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
874                                 spin_unlock(&direntry->d_lock);
875                         }
876
877                         return 1;
878                 }
879         }
880
881         /*
882          * This may be nfsd (or something), anyway, we can't see the
883          * intent of this. So, since this can be for creation, drop it.
884          */
885         if (!flags)
886                 return 0;
887
888         /*
889          * Drop the negative dentry, in order to make sure to use the
890          * case sensitive name which is specified by user if this is
891          * for creation.
892          */
893         if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
894                 return 0;
895
896         if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled)
897                 return 0;
898
899         return 1;
900 }
901
902 /* static int cifs_d_delete(struct dentry *direntry)
903 {
904         int rc = 0;
905
906         cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry);
907
908         return rc;
909 }     */
910
911 const struct dentry_operations cifs_dentry_ops = {
912         .d_revalidate = cifs_d_revalidate,
913         .d_automount = cifs_dfs_d_automount,
914 /* d_delete:       cifs_d_delete,      */ /* not needed except for debugging */
915 };
916
917 static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
918 {
919         struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
920         unsigned long hash;
921         wchar_t c;
922         int i, charlen;
923
924         hash = init_name_hash(dentry);
925         for (i = 0; i < q->len; i += charlen) {
926                 charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
927                 /* error out if we can't convert the character */
928                 if (unlikely(charlen < 0))
929                         return charlen;
930                 hash = partial_name_hash(cifs_toupper(c), hash);
931         }
932         q->hash = end_name_hash(hash);
933
934         return 0;
935 }
936
937 static int cifs_ci_compare(const struct dentry *dentry,
938                 unsigned int len, const char *str, const struct qstr *name)
939 {
940         struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
941         wchar_t c1, c2;
942         int i, l1, l2;
943
944         /*
945          * We make the assumption here that uppercase characters in the local
946          * codepage are always the same length as their lowercase counterparts.
947          *
948          * If that's ever not the case, then this will fail to match it.
949          */
950         if (name->len != len)
951                 return 1;
952
953         for (i = 0; i < len; i += l1) {
954                 /* Convert characters in both strings to UTF-16. */
955                 l1 = codepage->char2uni(&str[i], len - i, &c1);
956                 l2 = codepage->char2uni(&name->name[i], name->len - i, &c2);
957
958                 /*
959                  * If we can't convert either character, just declare it to
960                  * be 1 byte long and compare the original byte.
961                  */
962                 if (unlikely(l1 < 0 && l2 < 0)) {
963                         if (str[i] != name->name[i])
964                                 return 1;
965                         l1 = 1;
966                         continue;
967                 }
968
969                 /*
970                  * Here, we again ass|u|me that upper/lowercase versions of
971                  * a character are the same length in the local NLS.
972                  */
973                 if (l1 != l2)
974                         return 1;
975
976                 /* Now compare uppercase versions of these characters */
977                 if (cifs_toupper(c1) != cifs_toupper(c2))
978                         return 1;
979         }
980
981         return 0;
982 }
983
984 const struct dentry_operations cifs_ci_dentry_ops = {
985         .d_revalidate = cifs_d_revalidate,
986         .d_hash = cifs_ci_hash,
987         .d_compare = cifs_ci_compare,
988         .d_automount = cifs_dfs_d_automount,
989 };