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