Merge tag 'regmap-v5.12' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6-microblaze.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 #include "fs_context.h"
37
38 static void
39 renew_parental_timestamps(struct dentry *direntry)
40 {
41         /* BB check if there is a way to get the kernel to do this or if we
42            really need this */
43         do {
44                 cifs_set_time(direntry, jiffies);
45                 direntry = direntry->d_parent;
46         } while (!IS_ROOT(direntry));
47 }
48
49 char *
50 cifs_build_path_to_root(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
51                         struct cifs_tcon *tcon, int add_treename)
52 {
53         int pplen = ctx->prepath ? strlen(ctx->prepath) + 1 : 0;
54         int dfsplen;
55         char *full_path = NULL;
56
57         /* if no prefix path, simply set path to the root of share to "" */
58         if (pplen == 0) {
59                 full_path = kzalloc(1, GFP_KERNEL);
60                 return full_path;
61         }
62
63         if (add_treename)
64                 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
65         else
66                 dfsplen = 0;
67
68         full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
69         if (full_path == NULL)
70                 return full_path;
71
72         if (dfsplen)
73                 memcpy(full_path, tcon->treeName, dfsplen);
74         full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb);
75         memcpy(full_path + dfsplen + 1, ctx->prepath, pplen);
76         convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
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 cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
85         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
86         bool prefix = tcon->Flags & SMB_SHARE_IS_IN_DFS;
87
88         return build_path_from_dentry_optional_prefix(direntry,
89                                                       prefix);
90 }
91
92 char *
93 build_path_from_dentry_optional_prefix(struct dentry *direntry, bool prefix)
94 {
95         struct dentry *temp;
96         int namelen;
97         int dfsplen;
98         int pplen = 0;
99         char *full_path;
100         char dirsep;
101         struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
102         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
103         unsigned seq;
104
105         dirsep = CIFS_DIR_SEP(cifs_sb);
106         if (prefix)
107                 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
108         else
109                 dfsplen = 0;
110
111         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
112                 pplen = cifs_sb->prepath ? strlen(cifs_sb->prepath) + 1 : 0;
113
114 cifs_bp_rename_retry:
115         namelen = dfsplen + pplen;
116         seq = read_seqbegin(&rename_lock);
117         rcu_read_lock();
118         for (temp = direntry; !IS_ROOT(temp);) {
119                 namelen += (1 + temp->d_name.len);
120                 temp = temp->d_parent;
121                 if (temp == NULL) {
122                         cifs_dbg(VFS, "corrupt dentry\n");
123                         rcu_read_unlock();
124                         return NULL;
125                 }
126         }
127         rcu_read_unlock();
128
129         full_path = kmalloc(namelen+1, GFP_ATOMIC);
130         if (full_path == NULL)
131                 return full_path;
132         full_path[namelen] = 0; /* trailing null */
133         rcu_read_lock();
134         for (temp = direntry; !IS_ROOT(temp);) {
135                 spin_lock(&temp->d_lock);
136                 namelen -= 1 + temp->d_name.len;
137                 if (namelen < 0) {
138                         spin_unlock(&temp->d_lock);
139                         break;
140                 } else {
141                         full_path[namelen] = dirsep;
142                         strncpy(full_path + namelen + 1, temp->d_name.name,
143                                 temp->d_name.len);
144                         cifs_dbg(FYI, "name: %s\n", full_path + namelen);
145                 }
146                 spin_unlock(&temp->d_lock);
147                 temp = temp->d_parent;
148                 if (temp == NULL) {
149                         cifs_dbg(VFS, "corrupt dentry\n");
150                         rcu_read_unlock();
151                         kfree(full_path);
152                         return NULL;
153                 }
154         }
155         rcu_read_unlock();
156         if (namelen != dfsplen + pplen || read_seqretry(&rename_lock, seq)) {
157                 cifs_dbg(FYI, "did not end path lookup where expected. namelen=%ddfsplen=%d\n",
158                          namelen, dfsplen);
159                 /* presumably this is only possible if racing with a rename
160                 of one of the parent directories  (we can not lock the dentries
161                 above us to prevent this, but retrying should be harmless) */
162                 kfree(full_path);
163                 goto cifs_bp_rename_retry;
164         }
165         /* DIR_SEP already set for byte  0 / vs \ but not for
166            subsequent slashes in prepath which currently must
167            be entered the right way - not sure if there is an alternative
168            since the '\' is a valid posix character so we can not switch
169            those safely to '/' if any are found in the middle of the prepath */
170         /* BB test paths to Windows with '/' in the midst of prepath */
171
172         if (pplen) {
173                 int i;
174
175                 cifs_dbg(FYI, "using cifs_sb prepath <%s>\n", cifs_sb->prepath);
176                 memcpy(full_path+dfsplen+1, cifs_sb->prepath, pplen-1);
177                 full_path[dfsplen] = dirsep;
178                 for (i = 0; i < pplen-1; i++)
179                         if (full_path[dfsplen+1+i] == '/')
180                                 full_path[dfsplen+1+i] = CIFS_DIR_SEP(cifs_sb);
181         }
182
183         if (dfsplen) {
184                 strncpy(full_path, tcon->treeName, dfsplen);
185                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
186                         int i;
187                         for (i = 0; i < dfsplen; i++) {
188                                 if (full_path[i] == '\\')
189                                         full_path[i] = '/';
190                         }
191                 }
192         }
193         return full_path;
194 }
195
196 /*
197  * Don't allow path components longer than the server max.
198  * Don't allow the separator character in a path component.
199  * The VFS will not allow "/", but "\" is allowed by posix.
200  */
201 static int
202 check_name(struct dentry *direntry, struct cifs_tcon *tcon)
203 {
204         struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
205         int i;
206
207         if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength &&
208                      direntry->d_name.len >
209                      le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength)))
210                 return -ENAMETOOLONG;
211
212         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
213                 for (i = 0; i < direntry->d_name.len; i++) {
214                         if (direntry->d_name.name[i] == '\\') {
215                                 cifs_dbg(FYI, "Invalid file name\n");
216                                 return -EINVAL;
217                         }
218                 }
219         }
220         return 0;
221 }
222
223
224 /* Inode operations in similar order to how they appear in Linux file fs.h */
225
226 static int
227 cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid,
228                struct tcon_link *tlink, unsigned oflags, umode_t mode,
229                __u32 *oplock, struct cifs_fid *fid)
230 {
231         int rc = -ENOENT;
232         int create_options = CREATE_NOT_DIR;
233         int desired_access;
234         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
235         struct cifs_tcon *tcon = tlink_tcon(tlink);
236         char *full_path = NULL;
237         FILE_ALL_INFO *buf = NULL;
238         struct inode *newinode = NULL;
239         int disposition;
240         struct TCP_Server_Info *server = tcon->ses->server;
241         struct cifs_open_parms oparms;
242
243         *oplock = 0;
244         if (tcon->ses->server->oplocks)
245                 *oplock = REQ_OPLOCK;
246
247         full_path = build_path_from_dentry(direntry);
248         if (!full_path)
249                 return -ENOMEM;
250
251         if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open &&
252             (CIFS_UNIX_POSIX_PATH_OPS_CAP &
253                         le64_to_cpu(tcon->fsUnixInfo.Capability))) {
254                 rc = cifs_posix_open(full_path, &newinode, inode->i_sb, mode,
255                                      oflags, oplock, &fid->netfid, xid);
256                 switch (rc) {
257                 case 0:
258                         if (newinode == NULL) {
259                                 /* query inode info */
260                                 goto cifs_create_get_file_info;
261                         }
262
263                         if (S_ISDIR(newinode->i_mode)) {
264                                 CIFSSMBClose(xid, tcon, fid->netfid);
265                                 iput(newinode);
266                                 rc = -EISDIR;
267                                 goto out;
268                         }
269
270                         if (!S_ISREG(newinode->i_mode)) {
271                                 /*
272                                  * The server may allow us to open things like
273                                  * FIFOs, but the client isn't set up to deal
274                                  * with that. If it's not a regular file, just
275                                  * close it and proceed as if it were a normal
276                                  * lookup.
277                                  */
278                                 CIFSSMBClose(xid, tcon, fid->netfid);
279                                 goto cifs_create_get_file_info;
280                         }
281                         /* success, no need to query */
282                         goto cifs_create_set_dentry;
283
284                 case -ENOENT:
285                         goto cifs_create_get_file_info;
286
287                 case -EIO:
288                 case -EINVAL:
289                         /*
290                          * EIO could indicate that (posix open) operation is not
291                          * supported, despite what server claimed in capability
292                          * negotiation.
293                          *
294                          * POSIX open in samba versions 3.3.1 and earlier could
295                          * incorrectly fail with invalid parameter.
296                          */
297                         tcon->broken_posix_open = true;
298                         break;
299
300                 case -EREMOTE:
301                 case -EOPNOTSUPP:
302                         /*
303                          * EREMOTE indicates DFS junction, which is not handled
304                          * in posix open.  If either that or op not supported
305                          * returned, follow the normal lookup.
306                          */
307                         break;
308
309                 default:
310                         goto out;
311                 }
312                 /*
313                  * fallthrough to retry, using older open call, this is case
314                  * where server does not support this SMB level, and falsely
315                  * claims capability (also get here for DFS case which should be
316                  * rare for path not covered on files)
317                  */
318         }
319
320         desired_access = 0;
321         if (OPEN_FMODE(oflags) & FMODE_READ)
322                 desired_access |= GENERIC_READ; /* is this too little? */
323         if (OPEN_FMODE(oflags) & FMODE_WRITE)
324                 desired_access |= GENERIC_WRITE;
325
326         disposition = FILE_OVERWRITE_IF;
327         if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
328                 disposition = FILE_CREATE;
329         else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
330                 disposition = FILE_OVERWRITE_IF;
331         else if ((oflags & O_CREAT) == O_CREAT)
332                 disposition = FILE_OPEN_IF;
333         else
334                 cifs_dbg(FYI, "Create flag not set in create function\n");
335
336         /*
337          * BB add processing to set equivalent of mode - e.g. via CreateX with
338          * ACLs
339          */
340
341         if (!server->ops->open) {
342                 rc = -ENOSYS;
343                 goto out;
344         }
345
346         buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
347         if (buf == NULL) {
348                 rc = -ENOMEM;
349                 goto out;
350         }
351
352         /*
353          * if we're not using unix extensions, see if we need to set
354          * ATTR_READONLY on the create call
355          */
356         if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
357                 create_options |= CREATE_OPTION_READONLY;
358
359         oparms.tcon = tcon;
360         oparms.cifs_sb = cifs_sb;
361         oparms.desired_access = desired_access;
362         oparms.create_options = cifs_create_options(cifs_sb, create_options);
363         oparms.disposition = disposition;
364         oparms.path = full_path;
365         oparms.fid = fid;
366         oparms.reconnect = false;
367         oparms.mode = mode;
368         rc = server->ops->open(xid, &oparms, oplock, buf);
369         if (rc) {
370                 cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc);
371                 goto out;
372         }
373
374         /*
375          * If Open reported that we actually created a file then we now have to
376          * set the mode if possible.
377          */
378         if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) {
379                 struct cifs_unix_set_info_args args = {
380                                 .mode   = mode,
381                                 .ctime  = NO_CHANGE_64,
382                                 .atime  = NO_CHANGE_64,
383                                 .mtime  = NO_CHANGE_64,
384                                 .device = 0,
385                 };
386
387                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
388                         args.uid = current_fsuid();
389                         if (inode->i_mode & S_ISGID)
390                                 args.gid = inode->i_gid;
391                         else
392                                 args.gid = current_fsgid();
393                 } else {
394                         args.uid = INVALID_UID; /* no change */
395                         args.gid = INVALID_GID; /* no change */
396                 }
397                 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid,
398                                        current->tgid);
399         } else {
400                 /*
401                  * BB implement mode setting via Windows security
402                  * descriptors e.g.
403                  */
404                 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
405
406                 /* Could set r/o dos attribute if mode & 0222 == 0 */
407         }
408
409 cifs_create_get_file_info:
410         /* server might mask mode so we have to query for it */
411         if (tcon->unix_ext)
412                 rc = cifs_get_inode_info_unix(&newinode, full_path, inode->i_sb,
413                                               xid);
414         else {
415                 /* TODO: Add support for calling POSIX query info here, but passing in fid */
416                 rc = cifs_get_inode_info(&newinode, full_path, buf, inode->i_sb,
417                                          xid, fid);
418                 if (newinode) {
419                         if (server->ops->set_lease_key)
420                                 server->ops->set_lease_key(newinode, fid);
421                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
422                                 newinode->i_mode = mode;
423                         if ((*oplock & CIFS_CREATE_ACTION) &&
424                             (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
425                                 newinode->i_uid = current_fsuid();
426                                 if (inode->i_mode & S_ISGID)
427                                         newinode->i_gid = inode->i_gid;
428                                 else
429                                         newinode->i_gid = current_fsgid();
430                         }
431                 }
432         }
433
434 cifs_create_set_dentry:
435         if (rc != 0) {
436                 cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n",
437                          rc);
438                 goto out_err;
439         }
440
441         if (S_ISDIR(newinode->i_mode)) {
442                 rc = -EISDIR;
443                 goto out_err;
444         }
445
446         d_drop(direntry);
447         d_add(direntry, newinode);
448
449 out:
450         kfree(buf);
451         kfree(full_path);
452         return rc;
453
454 out_err:
455         if (server->ops->close)
456                 server->ops->close(xid, tcon, fid);
457         if (newinode)
458                 iput(newinode);
459         goto out;
460 }
461
462 int
463 cifs_atomic_open(struct inode *inode, struct dentry *direntry,
464                  struct file *file, unsigned oflags, umode_t mode)
465 {
466         int rc;
467         unsigned int xid;
468         struct tcon_link *tlink;
469         struct cifs_tcon *tcon;
470         struct TCP_Server_Info *server;
471         struct cifs_fid fid;
472         struct cifs_pending_open open;
473         __u32 oplock;
474         struct cifsFileInfo *file_info;
475
476         /*
477          * Posix open is only called (at lookup time) for file create now. For
478          * opens (rather than creates), because we do not know if it is a file
479          * or directory yet, and current Samba no longer allows us to do posix
480          * open on dirs, we could end up wasting an open call on what turns out
481          * to be a dir. For file opens, we wait to call posix open till
482          * cifs_open.  It could be added to atomic_open in the future but the
483          * performance tradeoff of the extra network request when EISDIR or
484          * EACCES is returned would have to be weighed against the 50% reduction
485          * in network traffic in the other paths.
486          */
487         if (!(oflags & O_CREAT)) {
488                 struct dentry *res;
489
490                 /*
491                  * Check for hashed negative dentry. We have already revalidated
492                  * the dentry and it is fine. No need to perform another lookup.
493                  */
494                 if (!d_in_lookup(direntry))
495                         return -ENOENT;
496
497                 res = cifs_lookup(inode, direntry, 0);
498                 if (IS_ERR(res))
499                         return PTR_ERR(res);
500
501                 return finish_no_open(file, res);
502         }
503
504         xid = get_xid();
505
506         cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
507                  inode, direntry, direntry);
508
509         tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
510         if (IS_ERR(tlink)) {
511                 rc = PTR_ERR(tlink);
512                 goto out_free_xid;
513         }
514
515         tcon = tlink_tcon(tlink);
516
517         rc = check_name(direntry, tcon);
518         if (rc)
519                 goto out;
520
521         server = tcon->ses->server;
522
523         if (server->ops->new_lease_key)
524                 server->ops->new_lease_key(&fid);
525
526         cifs_add_pending_open(&fid, tlink, &open);
527
528         rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
529                             &oplock, &fid);
530
531         if (rc) {
532                 cifs_del_pending_open(&open);
533                 goto out;
534         }
535
536         if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
537                 file->f_mode |= FMODE_CREATED;
538
539         rc = finish_open(file, direntry, generic_file_open);
540         if (rc) {
541                 if (server->ops->close)
542                         server->ops->close(xid, tcon, &fid);
543                 cifs_del_pending_open(&open);
544                 goto out;
545         }
546
547         if (file->f_flags & O_DIRECT &&
548             CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
549                 if (CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
550                         file->f_op = &cifs_file_direct_nobrl_ops;
551                 else
552                         file->f_op = &cifs_file_direct_ops;
553                 }
554
555         file_info = cifs_new_fileinfo(&fid, file, tlink, oplock);
556         if (file_info == NULL) {
557                 if (server->ops->close)
558                         server->ops->close(xid, tcon, &fid);
559                 cifs_del_pending_open(&open);
560                 rc = -ENOMEM;
561         }
562
563 out:
564         cifs_put_tlink(tlink);
565 out_free_xid:
566         free_xid(xid);
567         return rc;
568 }
569
570 int cifs_create(struct inode *inode, struct dentry *direntry, umode_t mode,
571                 bool excl)
572 {
573         int rc;
574         unsigned int xid = get_xid();
575         /*
576          * BB below access is probably too much for mknod to request
577          *    but we have to do query and setpathinfo so requesting
578          *    less could fail (unless we want to request getatr and setatr
579          *    permissions (only).  At least for POSIX we do not have to
580          *    request so much.
581          */
582         unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
583         struct tcon_link *tlink;
584         struct cifs_tcon *tcon;
585         struct TCP_Server_Info *server;
586         struct cifs_fid fid;
587         __u32 oplock;
588
589         cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
590                  inode, direntry, direntry);
591
592         tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
593         rc = PTR_ERR(tlink);
594         if (IS_ERR(tlink))
595                 goto out_free_xid;
596
597         tcon = tlink_tcon(tlink);
598         server = tcon->ses->server;
599
600         if (server->ops->new_lease_key)
601                 server->ops->new_lease_key(&fid);
602
603         rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
604                             &oplock, &fid);
605         if (!rc && server->ops->close)
606                 server->ops->close(xid, tcon, &fid);
607
608         cifs_put_tlink(tlink);
609 out_free_xid:
610         free_xid(xid);
611         return rc;
612 }
613
614 int cifs_mknod(struct inode *inode, struct dentry *direntry, umode_t mode,
615                 dev_t device_number)
616 {
617         int rc = -EPERM;
618         unsigned int xid;
619         struct cifs_sb_info *cifs_sb;
620         struct tcon_link *tlink;
621         struct cifs_tcon *tcon;
622         char *full_path = NULL;
623
624         if (!old_valid_dev(device_number))
625                 return -EINVAL;
626
627         cifs_sb = CIFS_SB(inode->i_sb);
628         tlink = cifs_sb_tlink(cifs_sb);
629         if (IS_ERR(tlink))
630                 return PTR_ERR(tlink);
631
632         tcon = tlink_tcon(tlink);
633
634         xid = get_xid();
635
636         full_path = build_path_from_dentry(direntry);
637         if (full_path == NULL) {
638                 rc = -ENOMEM;
639                 goto mknod_out;
640         }
641
642         rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon,
643                                                full_path, mode,
644                                                device_number);
645
646 mknod_out:
647         kfree(full_path);
648         free_xid(xid);
649         cifs_put_tlink(tlink);
650         return rc;
651 }
652
653 struct dentry *
654 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
655             unsigned int flags)
656 {
657         unsigned int xid;
658         int rc = 0; /* to get around spurious gcc warning, set to zero here */
659         struct cifs_sb_info *cifs_sb;
660         struct tcon_link *tlink;
661         struct cifs_tcon *pTcon;
662         struct inode *newInode = NULL;
663         char *full_path = NULL;
664
665         xid = get_xid();
666
667         cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
668                  parent_dir_inode, direntry, direntry);
669
670         /* check whether path exists */
671
672         cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
673         tlink = cifs_sb_tlink(cifs_sb);
674         if (IS_ERR(tlink)) {
675                 free_xid(xid);
676                 return ERR_CAST(tlink);
677         }
678         pTcon = tlink_tcon(tlink);
679
680         rc = check_name(direntry, pTcon);
681         if (unlikely(rc)) {
682                 cifs_put_tlink(tlink);
683                 free_xid(xid);
684                 return ERR_PTR(rc);
685         }
686
687         /* can not grab the rename sem here since it would
688         deadlock in the cases (beginning of sys_rename itself)
689         in which we already have the sb rename sem */
690         full_path = build_path_from_dentry(direntry);
691         if (full_path == NULL) {
692                 cifs_put_tlink(tlink);
693                 free_xid(xid);
694                 return ERR_PTR(-ENOMEM);
695         }
696
697         if (d_really_is_positive(direntry)) {
698                 cifs_dbg(FYI, "non-NULL inode in lookup\n");
699         } else {
700                 cifs_dbg(FYI, "NULL inode in lookup\n");
701         }
702         cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
703                  full_path, d_inode(direntry));
704
705         if (pTcon->posix_extensions)
706                 rc = smb311_posix_get_inode_info(&newInode, full_path, parent_dir_inode->i_sb, xid);
707         else if (pTcon->unix_ext) {
708                 rc = cifs_get_inode_info_unix(&newInode, full_path,
709                                               parent_dir_inode->i_sb, xid);
710         } else {
711                 rc = cifs_get_inode_info(&newInode, full_path, NULL,
712                                 parent_dir_inode->i_sb, xid, NULL);
713         }
714
715         if (rc == 0) {
716                 /* since paths are not looked up by component - the parent
717                    directories are presumed to be good here */
718                 renew_parental_timestamps(direntry);
719         } else if (rc == -ENOENT) {
720                 cifs_set_time(direntry, jiffies);
721                 newInode = NULL;
722         } else {
723                 if (rc != -EACCES) {
724                         cifs_dbg(FYI, "Unexpected lookup error %d\n", rc);
725                         /* We special case check for Access Denied - since that
726                         is a common return code */
727                 }
728                 newInode = ERR_PTR(rc);
729         }
730         kfree(full_path);
731         cifs_put_tlink(tlink);
732         free_xid(xid);
733         return d_splice_alias(newInode, direntry);
734 }
735
736 static int
737 cifs_d_revalidate(struct dentry *direntry, unsigned int flags)
738 {
739         struct inode *inode;
740         int rc;
741
742         if (flags & LOOKUP_RCU)
743                 return -ECHILD;
744
745         if (d_really_is_positive(direntry)) {
746                 inode = d_inode(direntry);
747                 if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode)))
748                         CIFS_I(inode)->time = 0; /* force reval */
749
750                 rc = cifs_revalidate_dentry(direntry);
751                 if (rc) {
752                         cifs_dbg(FYI, "cifs_revalidate_dentry failed with rc=%d", rc);
753                         switch (rc) {
754                         case -ENOENT:
755                         case -ESTALE:
756                                 /*
757                                  * Those errors mean the dentry is invalid
758                                  * (file was deleted or recreated)
759                                  */
760                                 return 0;
761                         default:
762                                 /*
763                                  * Otherwise some unexpected error happened
764                                  * report it as-is to VFS layer
765                                  */
766                                 return rc;
767                         }
768                 }
769                 else {
770                         /*
771                          * If the inode wasn't known to be a dfs entry when
772                          * the dentry was instantiated, such as when created
773                          * via ->readdir(), it needs to be set now since the
774                          * attributes will have been updated by
775                          * cifs_revalidate_dentry().
776                          */
777                         if (IS_AUTOMOUNT(inode) &&
778                            !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
779                                 spin_lock(&direntry->d_lock);
780                                 direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
781                                 spin_unlock(&direntry->d_lock);
782                         }
783
784                         return 1;
785                 }
786         }
787
788         /*
789          * This may be nfsd (or something), anyway, we can't see the
790          * intent of this. So, since this can be for creation, drop it.
791          */
792         if (!flags)
793                 return 0;
794
795         /*
796          * Drop the negative dentry, in order to make sure to use the
797          * case sensitive name which is specified by user if this is
798          * for creation.
799          */
800         if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
801                 return 0;
802
803         if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled)
804                 return 0;
805
806         return 1;
807 }
808
809 /* static int cifs_d_delete(struct dentry *direntry)
810 {
811         int rc = 0;
812
813         cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry);
814
815         return rc;
816 }     */
817
818 const struct dentry_operations cifs_dentry_ops = {
819         .d_revalidate = cifs_d_revalidate,
820         .d_automount = cifs_dfs_d_automount,
821 /* d_delete:       cifs_d_delete,      */ /* not needed except for debugging */
822 };
823
824 static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
825 {
826         struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
827         unsigned long hash;
828         wchar_t c;
829         int i, charlen;
830
831         hash = init_name_hash(dentry);
832         for (i = 0; i < q->len; i += charlen) {
833                 charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
834                 /* error out if we can't convert the character */
835                 if (unlikely(charlen < 0))
836                         return charlen;
837                 hash = partial_name_hash(cifs_toupper(c), hash);
838         }
839         q->hash = end_name_hash(hash);
840
841         return 0;
842 }
843
844 static int cifs_ci_compare(const struct dentry *dentry,
845                 unsigned int len, const char *str, const struct qstr *name)
846 {
847         struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
848         wchar_t c1, c2;
849         int i, l1, l2;
850
851         /*
852          * We make the assumption here that uppercase characters in the local
853          * codepage are always the same length as their lowercase counterparts.
854          *
855          * If that's ever not the case, then this will fail to match it.
856          */
857         if (name->len != len)
858                 return 1;
859
860         for (i = 0; i < len; i += l1) {
861                 /* Convert characters in both strings to UTF-16. */
862                 l1 = codepage->char2uni(&str[i], len - i, &c1);
863                 l2 = codepage->char2uni(&name->name[i], name->len - i, &c2);
864
865                 /*
866                  * If we can't convert either character, just declare it to
867                  * be 1 byte long and compare the original byte.
868                  */
869                 if (unlikely(l1 < 0 && l2 < 0)) {
870                         if (str[i] != name->name[i])
871                                 return 1;
872                         l1 = 1;
873                         continue;
874                 }
875
876                 /*
877                  * Here, we again ass|u|me that upper/lowercase versions of
878                  * a character are the same length in the local NLS.
879                  */
880                 if (l1 != l2)
881                         return 1;
882
883                 /* Now compare uppercase versions of these characters */
884                 if (cifs_toupper(c1) != cifs_toupper(c2))
885                         return 1;
886         }
887
888         return 0;
889 }
890
891 const struct dentry_operations cifs_ci_dentry_ops = {
892         .d_revalidate = cifs_d_revalidate,
893         .d_hash = cifs_ci_hash,
894         .d_compare = cifs_ci_compare,
895         .d_automount = cifs_dfs_d_automount,
896 };