GNU Linux-libre 4.4.285-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                 direntry->d_time = 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_unhashed(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                 fput(file);
555                 rc = -ENOMEM;
556         }
557
558 out:
559         cifs_put_tlink(tlink);
560 out_free_xid:
561         free_xid(xid);
562         return rc;
563 }
564
565 int cifs_create(struct inode *inode, struct dentry *direntry, umode_t mode,
566                 bool excl)
567 {
568         int rc;
569         unsigned int xid = get_xid();
570         /*
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.
576          */
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;
583
584         cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
585                  inode, direntry, direntry);
586
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;
591
592         tcon = tlink_tcon(tlink);
593         server = tcon->ses->server;
594
595         if (server->ops->new_lease_key)
596                 server->ops->new_lease_key(&fid);
597
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);
602
603         cifs_put_tlink(tlink);
604 out_free_xid:
605         free_xid(xid);
606         return rc;
607 }
608
609 int cifs_mknod(struct inode *inode, struct dentry *direntry, umode_t mode,
610                 dev_t device_number)
611 {
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];
628
629         if (!old_valid_dev(device_number))
630                 return -EINVAL;
631
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);
636
637         tcon = tlink_tcon(tlink);
638
639         xid = get_xid();
640
641         full_path = build_path_from_dentry(direntry);
642         if (full_path == NULL) {
643                 rc = -ENOMEM;
644                 goto mknod_out;
645         }
646
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,
654                 };
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 */
661                 }
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;
667
668                 rc = cifs_get_inode_info_unix(&newinode, full_path,
669                                                 inode->i_sb, xid);
670
671                 if (rc == 0)
672                         d_instantiate(direntry, newinode);
673                 goto mknod_out;
674         }
675
676         if (!S_ISCHR(mode) && !S_ISBLK(mode))
677                 goto mknod_out;
678
679         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
680                 goto mknod_out;
681
682
683         cifs_dbg(FYI, "sfu compat create special file\n");
684
685         buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
686         if (buf == NULL) {
687                 rc = -ENOMEM;
688                 goto mknod_out;
689         }
690
691         if (backup_cred(cifs_sb))
692                 create_options |= CREATE_OPEN_BACKUP_INTENT;
693
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;
702
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;
710
711         /*
712          * BB Do not bother to decode buf since no local inode yet to put
713          * timestamps in, but we can reuse it safely.
714          */
715
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);
735         }
736         tcon->ses->server->ops->close(xid, tcon, &fid);
737         d_drop(direntry);
738
739         /* FIXME: add code here to set EAs */
740
741 mknod_out:
742         kfree(full_path);
743         kfree(buf);
744         free_xid(xid);
745         cifs_put_tlink(tlink);
746         return rc;
747 }
748
749 struct dentry *
750 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
751             unsigned int flags)
752 {
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;
760
761         xid = get_xid();
762
763         cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
764                  parent_dir_inode, direntry, direntry);
765
766         /* check whether path exists */
767
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;
773         }
774         pTcon = tlink_tcon(tlink);
775
776         rc = check_name(direntry, pTcon);
777         if (rc)
778                 goto lookup_out;
779
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;
787         }
788
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");
793         }
794         cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
795                  full_path, d_inode(direntry));
796
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);
803         }
804
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);
810
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 */
821         }
822
823 lookup_out:
824         kfree(full_path);
825         cifs_put_tlink(tlink);
826         free_xid(xid);
827         return ERR_PTR(rc);
828 }
829
830 static int
831 cifs_d_revalidate(struct dentry *direntry, unsigned int flags)
832 {
833         struct inode *inode;
834         int rc;
835
836         if (flags & LOOKUP_RCU)
837                 return -ECHILD;
838
839         if (d_really_is_positive(direntry)) {
840                 inode = d_inode(direntry);
841                 if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode)))
842                         CIFS_I(inode)->time = 0; /* force reval */
843
844                 rc = cifs_revalidate_dentry(direntry);
845                 if (rc) {
846                         cifs_dbg(FYI, "cifs_revalidate_dentry failed with rc=%d", rc);
847                         switch (rc) {
848                         case -ENOENT:
849                         case -ESTALE:
850                                 /*
851                                  * Those errors mean the dentry is invalid
852                                  * (file was deleted or recreated)
853                                  */
854                                 return 0;
855                         default:
856                                 /*
857                                  * Otherwise some unexpected error happened
858                                  * report it as-is to VFS layer
859                                  */
860                                 return rc;
861                         }
862                 }
863                 else {
864                         /*
865                          * If the inode wasn't known to be a dfs entry when
866                          * the dentry was instantiated, such as when created
867                          * via ->readdir(), it needs to be set now since the
868                          * attributes will have been updated by
869                          * cifs_revalidate_dentry().
870                          */
871                         if (IS_AUTOMOUNT(inode) &&
872                            !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
873                                 spin_lock(&direntry->d_lock);
874                                 direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
875                                 spin_unlock(&direntry->d_lock);
876                         }
877
878                         return 1;
879                 }
880         }
881
882         /*
883          * This may be nfsd (or something), anyway, we can't see the
884          * intent of this. So, since this can be for creation, drop it.
885          */
886         if (!flags)
887                 return 0;
888
889         /*
890          * Drop the negative dentry, in order to make sure to use the
891          * case sensitive name which is specified by user if this is
892          * for creation.
893          */
894         if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
895                 return 0;
896
897         if (time_after(jiffies, direntry->d_time + HZ) || !lookupCacheEnabled)
898                 return 0;
899
900         return 1;
901 }
902
903 /* static int cifs_d_delete(struct dentry *direntry)
904 {
905         int rc = 0;
906
907         cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry);
908
909         return rc;
910 }     */
911
912 const struct dentry_operations cifs_dentry_ops = {
913         .d_revalidate = cifs_d_revalidate,
914         .d_automount = cifs_dfs_d_automount,
915 /* d_delete:       cifs_d_delete,      */ /* not needed except for debugging */
916 };
917
918 static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
919 {
920         struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
921         unsigned long hash;
922         wchar_t c;
923         int i, charlen;
924
925         hash = init_name_hash();
926         for (i = 0; i < q->len; i += charlen) {
927                 charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
928                 /* error out if we can't convert the character */
929                 if (unlikely(charlen < 0))
930                         return charlen;
931                 hash = partial_name_hash(cifs_toupper(c), hash);
932         }
933         q->hash = end_name_hash(hash);
934
935         return 0;
936 }
937
938 static int cifs_ci_compare(const struct dentry *parent, const struct dentry *dentry,
939                 unsigned int len, const char *str, const struct qstr *name)
940 {
941         struct nls_table *codepage = CIFS_SB(parent->d_sb)->local_nls;
942         wchar_t c1, c2;
943         int i, l1, l2;
944
945         /*
946          * We make the assumption here that uppercase characters in the local
947          * codepage are always the same length as their lowercase counterparts.
948          *
949          * If that's ever not the case, then this will fail to match it.
950          */
951         if (name->len != len)
952                 return 1;
953
954         for (i = 0; i < len; i += l1) {
955                 /* Convert characters in both strings to UTF-16. */
956                 l1 = codepage->char2uni(&str[i], len - i, &c1);
957                 l2 = codepage->char2uni(&name->name[i], name->len - i, &c2);
958
959                 /*
960                  * If we can't convert either character, just declare it to
961                  * be 1 byte long and compare the original byte.
962                  */
963                 if (unlikely(l1 < 0 && l2 < 0)) {
964                         if (str[i] != name->name[i])
965                                 return 1;
966                         l1 = 1;
967                         continue;
968                 }
969
970                 /*
971                  * Here, we again ass|u|me that upper/lowercase versions of
972                  * a character are the same length in the local NLS.
973                  */
974                 if (l1 != l2)
975                         return 1;
976
977                 /* Now compare uppercase versions of these characters */
978                 if (cifs_toupper(c1) != cifs_toupper(c2))
979                         return 1;
980         }
981
982         return 0;
983 }
984
985 const struct dentry_operations cifs_ci_dentry_ops = {
986         .d_revalidate = cifs_d_revalidate,
987         .d_hash = cifs_ci_hash,
988         .d_compare = cifs_ci_compare,
989         .d_automount = cifs_dfs_d_automount,
990 };